Android onResume update list adapter

James Gu picture James Gu · Jan 25, 2012 · Viewed 28k times · Source

I'm using a list adapter to show different stores, when someone selects a store it takes them to a new activity where they can add the store to favorite on that screen.

There is a Back button on that calls finish(); that goes back to the screen with the listview.

Now the problem is the listview isn't updated (ie. doesn't show that the store is added to favorite already). I tried this code but no luck:

@Override
public void onResume() {
    super.onResume();
    list.setAdapter(null);      
    updateMyList();
    adapter=new LazyAdapter(this, ((String[])names.toArray(new String[0])), 
        ((String[])status.toArray(new String[0])));
    list.setAdapter(adapter);
}

updateMyList() calls the server API and updates the names and status arrays.

With this code the list doesn't really update...

Answer

Lalit Poptani picture Lalit Poptani · Jan 25, 2012

You should setAdapter in your onCreate() only, inside onResume() you just had to call adapter.notifyDataSetChanged() with the new collection of data. This will refresh your ListView with the new collection of data.