I have a GridView which is pretty similar to the Google tutorial, except that I want to add the ImageViews on runtime (via a subactivity). The results are okay, but the layo
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);
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);
@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.
You are placing the GridView
inside com.unimelb.pt3.ui.TransparentPanel
which is 10px
tall.
px
, you should use dp
.com.unimelb.pt3.ui.TransparentPanel
's android:layout_height="10px"
to android:layout_height="fill_parent"
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:)
In your adapter :
class MAdapter extends BaseAdapter{
List<Objects> mObjects;
...
public void clearAdapter(){
mObjects.clear();
}
public void addNewValues(List<Objects> mObjects){
this.mObjects = mObjects;
}
...
}
adapter.clearAdapter(); // clear old values
adapter.addNewValues(MObjects);
adapter.notifyDataChanged();