刷新GridView的内容自定义BaseAdapter [英] Refresh GridView contents with custom BaseAdapter

查看:216
本文介绍了刷新GridView的内容自定义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:

自定义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;
        }
    }

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

我认为这code将设置 GridView控件来在一开始,然后到适配器的内容的空视图。

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

我从他们抛出 removeView 功能不能被称为文档阅读不支持的异常。如何清除如果有任何网格视图的previous内容,并设置了新的内容?

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?

推荐答案

我解决我的问题与这片code的:

I solved my problem with this piece of code:

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

这篇关于刷新GridView的内容自定义BaseAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆