How to add item to listview at runtime

此生再无相见时 提交于 2019-12-08 12:32:54

问题


I want to know how to add a new text view to appearing ListView in android, ( for example. when I click on some button a new list item is added to the list view )?


回答1:


You simply add the object to the container that contains the objects displayed by the listview. Then you tell the listView adapter that a change has occured:

public void onClick(View v) {
list.add(someObject);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
setContentView(R.layout.list_view);



回答2:


notifyDataSetChanged is enough. I have used it. When I used setContentView(R.layout.list_view); my screen turned white. On removing setContentView(R.layout.list_view); it worked like a charm!!!



来源:https://stackoverflow.com/questions/7325437/how-to-add-item-to-listview-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!