views:

780

answers:

1

hello,I used ListView to dynamic add item,but there is a problem about not Smooth add. there are textView and button in my listActivity,Iwant to Press button ,then textView"s text can auto add to listView,but i Pressed button ,it donot work,unless after i enter content , press "OK"Key ,then Pressed button ,textView"s text can auto add to listView. I donot know why. if I continuous Pressed button ,as3 times, then press "Ok"Key ,the content auto add list View but 3 times.

 public class DynamicListItems extends ListActivity {
   private static final String   ITEM_KEY   = "key";
   ArrayList<HashMap<String, String>>   list= new ArrayList<HashMap<String, String>>();
private SimpleAdapter   adapter;
private EditText    newValue;@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.dynamic_list);
    newValue = (EditText) findViewById(R.id.new_value_field);

    setListAdapter(new SimpleAdapter(this, list, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value }));
    ((ImageButton) findViewById(R.id.button)).setOnClickListener(getBtnClickListener());
}

private OnClickListener getBtnClickListener() {
    return new OnClickListener() {
        public void onClick(View view) {
            try {

                HashMap<String, String> item = new HashMap<String, String>();
                item.put(ITEM_KEY, newValue.getText().toString());
                list.add(item);

                adapter.notifyDataSetChanged();
            } catch (NullPointerException e) {
                Log.i("[Dynamic Items]", "Tried to add null value");
            }
        }
    };
   }}

my another question is how to dynamic delete the item,which event i need to used,could you give me some directions or Code snippets? dynamic_list.xml only contains listView ,button,textView row.xml contains TextView .

i amm sorry i donot edit code together.

A: 

Is your getBtnClickListener method part of the ListActivity or ArrayAdapter class?

For me, when I update from the ListActivity class, I use this code...

// code to add a Contact to my database
// code to add a Contact to the list that
//   that is used by the ListView
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);

When I am updating from a method inside the ArrayAdapter class, I use this code...

 // from a LongPress on a ListView item
 convertView.setOnLongClickListener(new OnLongClickListener(){
     @Override
     public boolean onLongClick(View view) {
         view.performHapticFeedback(0, View.HAPTIC_FEEDBACK_ENABLED);
         // code to remove a Contact name from my database
         // code to remove that Contact name from my list
         //    that is used by the ListView
         ContactsAdapter.this.notifyDataSetChanged();
         return true;
     });
Eclipsed4utoo
thank you for your help, my getBtnClickListener method is part of the ListActivity ,which only a button but not belong to listView.i am not understand your code . my code can work,but I want to konw the reason I must press "oK" Press,then press button,after i enter content over in the textView. i think i donot need press"OK".
pengwang