Adham
Adham

Reputation: 64844

How to add item to listview at runtime

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 )?

Upvotes: 0

Views: 3884

Answers (2)

slayton
slayton

Reputation: 20319

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();

Upvotes: 3

Simon
Simon

Reputation: 11

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!!!

Upvotes: 0

Related Questions