使用自定义BaseAdapter刷新GridView内容

时间:2013-05-09 06:05:21

标签: android android-gridview baseadapter

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

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

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

2 个答案:

答案 0 :(得分: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只需要清除您的位图数组并重新填充它。

答案 1 :(得分:0)

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

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