Refresh GridView contents with custom BaseAdapter

此生再无相见时 提交于 2019-12-24 12:37:56

问题


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 ImageAdapter extends BaseAdapter {
        private static LayoutInflater mInflater;
        // Keep all Images in array
        private static Bitmap[] mThumbIds;
        private static int mViewResourceId, pos;
        private static CheckBox cb;
        // Constructor
        public ImageAdapter(Context ctx, int viewResourceId, Bitmap[] pics) {
            mInflater = (LayoutInflater) ctx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mThumbIds = pics;
            mViewResourceId = viewResourceId;
        }

        @Override
        public int getCount() {
            return mThumbIds.length;
        }

        @Override
        public Object getItem(int position) {
            return mThumbIds[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressWarnings("deprecation")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = mInflater.inflate(mViewResourceId, list, false);
            cb = (CheckBox) convertView.findViewById(R.id.select);
            Drawable background = new BitmapDrawable(mThumbIds[position]);
            cb.setBackgroundDrawable(background);
            pos = position;
            System.out.println("Setting checkbox set: "+imageIsDup[pos]);
            cb.setChecked(imageIsDup[pos]);
            System.out.println("Has checkbox been set? "+cb.isChecked());
            cb.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (cb.isChecked()) {
                        imageIsDup[pos] = true;
                    } else
                        imageIsDup[pos] = false;
                }
            });
            return convertView;
        }
    }

Code for setting the gridView:

final Dialog dialog = new Dialog(longOperationContext);
                    dialog.setContentView(R.layout.activity_list);
                    TextView no = (TextView) dialog
                            .findViewById(R.id.noOfDups);
                    no.setText("Found " + noOfImages
                            + " duplicates. Please verify.");
                    dialog.setTitle("Images Found");
                    dialog.setCancelable(false);
                    list = (GridView) dialog
                            .findViewById(R.id.grid_view);
                    ImageAdapter empty=new ImageAdapter(longOperationContext, R.layout.row, new Bitmap[0]);
                    imageAdapter = new ImageAdapter(
                            longOperationContext, R.layout.row, thumb);
                    dialog.show();
                    imageAdapter.notifyDataSetChanged();
                    list.invalidateViews();
                    list.setAdapter(empty);
                    list.setEmptyView(new View(longOperationContext));
                    list.invalidateViews();
                    list.setAdapter(imageAdapter);

I assumed that this code would set the gridView to an empty view in the beginning and then to the adapter's contents.

I read from the documentation that the removeView functions cannot be called as they throw an Unsupported Exception. How do I clear the previous contents of the grid view if any and set the new contents?


回答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.

@Override
public void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);

       mMyObjects = new ArrayList<Object>();
       mMyObject.add("StringObject"); // just an example
       mMyAdapter = new MyCustomAdapter(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.




回答2:


I solved my problem with this piece of code:

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


来源:https://stackoverflow.com/questions/16455429/refresh-gridview-contents-with-custom-baseadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!