刷新的GridView内容定制BaseAdapter(Refresh GridView conten

2019-10-18 03:16发布

我试图寻找这个问题的解决方案进行了两天,我难住了。

这是我到目前为止有:

自BaseAdapter类:

        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;
        }
    }

规范设置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);

我认为这个代码会在设定gridView中开始,然后到适配器的内容的空视图。

从我的文档阅读removeView因为他们抛出一个功能不能称为Unsupported Exception 。 如何清除如果有网格视图以前的内容,并设置了新的内容?

Answer 1:

在Android的清爽适配器的元素整体思想是使用对象的同一阵列只是重新填充它们。 例如,如果我有一个GridView像你的情况,我想重新填充的对象,你需要做的就是先申报对象的数组的事情:

private ArrayList<Object> mMyObjects;

填入数据,并创建您的适配器。

@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);
}

因此,我们填充对象数组,创造我们的适配器。 我们更新适配器/ GridView的孩子才刚刚重新装载您的阵列之前,应该做的事情:

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

并调用: mMyAdapter.notifySetDataChanged(); 这样做BaseAdapter知道有在出数据的变化和它的重绘的看法和你的ListView / GridView控件将获得新的项目更新。

所以你的情况,更新您GridView只需要明确你的位图的排列和重新填充它。



Answer 2:

我解决了我的问题,这一段代码:

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


文章来源: Refresh GridView contents with custom BaseAdapter