Skip to content Skip to sidebar Skip to footer

Refresh Gridview Contents With Custom Baseadapter

I've tried searching for a solution for this problem for a couple of days and I'm stumped. Here's what I have so far: Custom BaseAdapter class: public static class ImageAda

Solution 1:

The whole idea with refreshing adapter's elements in Android is just repopulate them using the same array of objects. For example if I have a GridView like in your case and I want to repopulate the objects the thing you need to do is declare an array of objects first :

private ArrayList<Object> mMyObjects;

populate it with data and create your adapter.

@OverridepublicvoidonCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);

       mMyObjects = newArrayList<Object>();
       mMyObject.add("StringObject"); // just an example
       mMyAdapter = newMyCustomAdapter(this, mMyObject);
       mMyGridView.setAdapter(mMyAdapter);
}

So we populate the array of objects and create our adapter. The thing we should do before updating the adapter / gridview's children is just repopulate your array :

mMyObjects.clear();
mMyObjects.add("NewStringObject");

and call : mMyAdapter.notifySetDataChanged(); Doing that BaseAdapter knows that there are changes in out data and it's redrawing it's views and your ListView / GridView will get updated with the new items.

So in your case, to update your GridView just need to clear your array of bitmaps and repopulate it.

Solution 2:

I solved my problem with this piece of code:

try{
                            imageAdapter.notifyDataSetChanged();
                        }
                        catch(NullPointerException e)
                        {
                            imageAdapter = new ImageAdapter(
                                    longOperationContext, R.layout.row, thumb);
                        }

Post a Comment for "Refresh Gridview Contents With Custom Baseadapter"