0

I am using custom adapter to display a ListView.

It is working fine.

But i need to add three items to the ListView. How do I add them and have them display?

I tried notifydatasetchanged() method that is not working.

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
Loganathan
  • 1,864
  • 3
  • 12
  • 17

2 Answers2

3

You have to add items to your list using list.add(...) then you tell the adapter with adapter.notifyDataSetChanged().

matiszz
  • 556
  • 3
  • 11
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
2

For adding item into the listview you must use the List/ArrayList object. Using this you can perform add/edit/delete operation on listview item data and this list set into your adapter to get the data from list/arraylist and set into list item.

Ok now suppose I have listview and want to add new item just add item into the list/arraylist object and notify to the adapter like this

List<String> myList = new ArrayList<String>();
ArrayAdapter<String> adapter;

oncreate(){
   // initi listview, adapter and set the myList object into adapter object and then set the adapter into listview


}

// on button click from somewhere
public void onClick(View view){
    myList.add("hello friends");// you can add your data here into list object
    adapter.notifyDataSetChanged();
}
Pratik
  • 30,639
  • 18
  • 84
  • 159