動(dòng)態(tài)向listView Android添加元素有人能解釋或建議一個(gè)教程來在Android中創(chuàng)建listView嗎?以下是我的要求:我應(yīng)該能夠通過按一個(gè)按鈕來動(dòng)態(tài)地添加新元素。應(yīng)該足夠簡(jiǎn)單地理解(例如,可能沒有任何性能改進(jìn)或轉(zhuǎn)換視圖)我知道這里有很多關(guān)于這個(gè)話題的問題,張貼在StackOverflow上,但是找不到任何能回答我問題的問題。謝謝!
3 回答

慕的地6264312
TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊
listItems.add("New Item");adapter.notifyDataSetChanged();
adapter.add("New Item");

BIG陽
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
private EditText editTxt;private Button btn;private ListView list;private ArrayAdapter<String> adapter;private ArrayList<String> arrayList;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTxt = (EditText) findViewById(R.id.editText); btn = (Button) findViewById(R.id.button); list = (ListView) findViewById(R.id.listView); arrayList = new ArrayList<String>(); // Adapter: You need three parameters 'the context, id of the layout (it will be where the data is shown), // and the array that contains the data adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList); // Here, you set the data in your ListView list.setAdapter(adapter); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // this line adds the data of your EditText and puts in your array arrayList.add(editTxt.getText().toString()); // next thing you have to do is check if your adapter has changed adapter.notifyDataSetChanged(); } });}
- 3 回答
- 0 關(guān)注
- 721 瀏覽
添加回答
舉報(bào)
0/150
提交
取消