Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android's ArrayAdapter - add strings listview instead of replace

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?

like image 207
Badr Hari Avatar asked Mar 22 '11 16:03

Badr Hari


People also ask

How to connect an adapter with list View?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.

How do you populate a ListView?

To add rows to a ListView you need to add it to your layout and implement an IListAdapter with methods that the ListView calls to populate itself. Android includes built-in ListActivity and ArrayAdapter classes that you can use without defining any custom layout XML or code.


1 Answers

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.

like image 133
Matthew Willis Avatar answered Sep 18 '22 14:09

Matthew Willis