问题
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