Android's ArrayAdapter - add strings listview instead of replace

Badr Hari picture Badr Hari · Mar 22, 2011 · Viewed 28.1k times · Source

This is how I add an array to listview:

ListView my_listview = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, my_array);
my_listview.setAdapter(adapter);

my_array is uri of a file.

It works well. But next next time I want to array a new array, then I don't want to replace with with new one instead add new ones to the existing ones. How can I accomplish that?

Answer

Matthew Willis picture Matthew Willis · Mar 22, 2011

You need to be using an ArrayList<String> my_array rather than a String[] my_array and then you can do:

my_array.addAll(other_array);

to add elements from a new array to your array. Then you can do:

adapter.notifyDataSetChanged();

to update your ListView with the new elements.