1

我已经尝试了几天寻找这个问题的解决方案,但我很难过。

这是我到目前为止所拥有的:

自定义 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. 如何清除网格视图的先前内容(如果有)并设置新内容?

4

2 回答 2

2

在 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只需要清除你的位图数组并重新填充它。

于 2013-05-09T07:24:19.983 回答
0

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

                    try{
                            imageAdapter.notifyDataSetChanged();
                        }
                        catch(NullPointerException e)
                        {
                            imageAdapter = new ImageAdapter(
                                    longOperationContext, R.layout.row, thumb);
                        }
于 2013-05-16T18:40:41.473 回答