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, as 3 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");
}
}
};
}}
How to dynamic delete the item ?
- dynamic_list.xml only contains listView ,button,textView
- row.xml contains
TextView
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…