动态向listView Android添加元素有人能解释或建议一个教程来在Android中创建listView吗?以下是我的要求:我应该能够通过按一个按钮来动态地添加新元素。应该足够简单地理解(例如,可能没有任何性能改进或转换视图)我知道这里有很多关于这个话题的问题,张贴在StackOverflow上,但是找不到任何能回答我问题的问题。谢谢!
3 回答
慕的地6264312
TA贡献1817条经验 获得超6个赞
listItems.add("New Item");adapter.notifyDataSetChanged();
adapter.add("New Item");
BIG阳
TA贡献1859条经验 获得超6个赞
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 关注
- 698 浏览
添加回答
举报
0/150
提交
取消