Search code examples
androidlistviewandroid-listview

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


Solution

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