android ArrayAdapter items update

Assuming itemTexts as String array or String ArrayList,where you are adding new items into itemsTextat that time after that you can call

mAdapter.notifyDataSetChanged();

If you did not get answer then please put some code.


I think something like this

public void updatedData(List itemsArrayList) {

    mAdapter.clear(); 

    if (itemsArrayList != null){

        for (Object object : itemsArrayList) {

            mAdapter.insert(object, mAdapter.getCount());
        }
    }

    mAdapter.notifyDataSetChanged();

}

Your problem is a typical Java error with pointers.

In a first step you are creating an array and passing this array to the adapter.

In the second step you are creating a new array (so new pointer is created) with new information but the adapter is still pointing to the original array.

// init itemsText var and pass to the adapter
String[] itemsText = {"123", "345", "567"};
mAdapter = new ArrayAdapter<String>(..., itemsText);

//ERROR HERE: itemsText variable will point to a new array instance
itemsText = {"789", "910", "1011"};

So, you can do two things, one, update the array contents instead of creating a new one:

//This will work for your example
items[0]="123";
items[1]="345";
items[2]="567";

... or what I would do, use a List, something like:

List<String> items= new ArrayList<String>(3);
boundedDevices.add("123");
boundedDevices.add("456");
boundedDevices.add("789");

And in the update:

boundedDevices.set("789");
boundedDevices.set("910");
boundedDevices.set("1011");

To add more information, in a real application normally you update the contents of the list adapter with information from a service or content provider, so normally to update the items you would do something like:

//clear the actual results
items.clear()

//add the results coming from a service
items.addAll(serviceResults);

With this you will clear the old results and load the new ones (think that the new results should have a different number of items).

And off course after update the data the call to notifyDataSetChanged();

If you have any doubt don't hesitate to comment.