How do you get the user to add to a list?
I want to make it so the user clicks a button, it opens up a dialog box, the user types in the name of an item, then it adds the item to a list. So far, I've gotten to where the dialog box opens up with an EditText, an OK and a Cancel button that dismisses the box. How do I do this? Here is my .java File:
package com.shoppinglist;
import android.app.ListActivity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ShoppingList extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1main = (Button) findViewById(R.id.Button01main);
button1main.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(ShoppingList.this);
dialog.setContentView(R.layout.maindialog);
dialog.setCancelable(true);
Button button = (Button) dialog.findViewById(R.id.cancel);
b开发者_如何学编程utton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
Thanks in advance!!!
You can create a listView to hold your items. Use an array adapter to hold the item objects. One easy way out is to create a ListActivity instead of Activity and set your "Add item" button as a header or footer view. This way you can always display that button no matter how many items you have.
Look at the SDK for examples on ListViews. There are plenty of them.
精彩评论