listView dynamic add item

Posted by pengwang on Stack Overflow See other posts from Stack Overflow or by pengwang
Published on 2010-01-04T09:43:38Z Indexed on 2010/05/24 23:01 UTC
Read the original article Hit count: 503

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about android

Related posts about listview