How to refresh a GridView?

Solution 1:

the GridView has an invalidateViews() method.

when you call this method: "all the views to be rebuilt and redrawn." http://developer.android.com/reference/android/widget/GridView.html

i think this is what you need:)

Solution 2:

You must, first tell the adapter to notify that the data has changed and set the adapter again to the grid

adapter.notifyDataChanged();
grid.setAdapter(adapter);

Solution 3:

This may be helpful. I refresh a gridview of book image thumbnails after a delete is executed on an item. Using adapter.notifyDataChanged(); as mentioned above didn't work for me as it's called in my adapter.

//this is a call that retrieves cached data.
//your constructor can be designed and used without it.
final Object data = getLastNonConfigurationInstance();

I essentially just reload the adapter and bind it to the same view.

//reload the adapter
adapter = new BooksAdapter(MyBooks.this, MyBooks.this, data, show_collection );
grid.invalidateViews();
grid.setAdapter(adapter);

Solution 4:

@flyerz @snagnever

Together you guys have got it. It should be:

adapter.notifyDataChanged();
grid.invalidateViews();

This will flag the adapter that its data has changed, which will then be propagated to the grid whenever after the invalidateViews() method is called.

Glad I found this question because I could not figure out how to add items to the grid after its been rendered.

Solution 5:

None of these answers actually worked for me and I had to mash all of them together. To actually get the GridView to update, you need to do this:

 adapter.notifyDataChanged();
 grid.invalidateViews();
 grid.setAdapter(adapter);

Hope this helps anyone who couldn't get the other solutions to work.