GridView的自定义ImageAdapter数据更改后不更新 [英] GridView with custom ImageAdapter doesn't update after dataset changes

查看:183
本文介绍了GridView的自定义ImageAdapter数据更改后不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施的图像与多个项目选择支持一个画廊。主要布局是一个 GridView控件它使用一个自定义的适配器。每个显示产品组合的ImageView + 复选框。如果多个复选框被选中我想简单地通过触摸在 GridView控件任何图像取消选中他们。我打电话 notifyDataSetChanged()实现此功能。不幸的是这种取消所有功能不起作用。我还是个新手,所以有可能是一些基本的东西,我不明白。可能有人告诉我,什么是错我的适配器?谢谢你。

I'm implementing a gallery of images with support for multiple item selection. The main layout is a GridView which uses a custom Adapter. Every displayed item is a combination ImageView + CheckBox. If several checkboxes are checked I want to uncheck them all simply by touching any image in the GridView. I'm calling notifyDataSetChanged() for implementing this functionality. Unfortunately this "uncheck all" feature doesn't work. I'm still a newbie so probably there is something basic that I don't understand. Could somebody tell me what is wrong with my Adapter? Thanks.

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // References to our images
    public Integer[] mThumbIds = {
            R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3
    };

    // The layout inflater used to retrieve gallery children layouts
    private LayoutInflater mInflater;

    // Array used to update checkboxes
    public boolean[] checkboxesState = new boolean[getCount()];

    public ImageAdapter(Context c) {
        mContext = c;
        mInflater = (LayoutInflater) 
                c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // Define the initial checked/unchecked state of the checkboxes
        for(int i = 0; i < getCount(); i++) {
            checkboxesState[i] = false;
        }
    }

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

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    /* Create a new View for each cell displayed in the GridView.
     * In our case a View is an instance of RelativeLayout from which
     * we can extract the image and checkbox to be displayed in the cell
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        View cell = convertView;
        if (cell == null) {
            // No View passed, create one.
            cell = mInflater.inflate(R.layout.galleryitem, null);
            // Setup the View content
            ImageView imageView = (ImageView)cell.findViewById(R.id.thumbImage);
            CheckBox checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox);
            checkBox.setChecked(checkboxesState[position]);
            checkBox.setId(position);
            imageView.setImageBitmap(downsample(mThumbIds[position]));

            // Setup the View behavior
            imageView.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // Uncheck all checked items
                    uncheckAll();
                }
            });

            checkBox.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    checkboxesState[cb.getId()] = cb.isChecked();
                }
            });
        }
        return cell;
    }

    private Bitmap downsample(int resId){
        // Some code here
    }

    private void uncheckAll() {
        // This code does not work. GridView is not refreshed
        for(int i = 0; i < checkboxesState.length; i++) {
            checkboxesState[i] = false;
        }
        notifyDataSetChanged();
    }
}

更新

我意识到,我的初始后并没有确切地描述了uncheckAll方法是如何失败的。当我有N个选中的复选框,并调用uncheckAll方法会发生什么事是:

I've realised that my initial post doesn't describe exactly how the uncheckAll method fails. What happens when I have N checked checkboxes and call the uncheckAll method is:


  • 最初选中的复选框都未选中

  • ñ复选框的新组检查

此行​​为使我想,也许在GridView物品,不料,改变他们在 GridView的位置所以我加了一个的TextView 用唯一的名称为每个项目。我的想法是正确的:当我滚动屏幕上的项目更改它们在 GridView控件位置。当uncheckAll叫同样的情况。我已经找到了解决方案,为我工作<一个href=\"http://stackoverflow.com/questions/6055730/gridview-elements-changes-their-place-dynamically-when-scroll-screen\">in这个线程。

This behavior made me to think that maybe the gridview items were, unexpectedly, changing their positions in the GridView so I added a TextView with a unique name to every item. My thought was correct: when I scrolled the screen the items change their position in the GridView. The same happens when uncheckAll is called. I've found a solution working for me in this thread.

请注意:我没有,因为在这个阶段,我是用每<$ C $相同的ImageView 资源开发实现约在开始运动问题C> GridView控件项目。

Note: I didn't realise about the moving problem at the very beginning because at this stage of development I was using the same ImageView resource for every GridView item.

推荐答案

问题是最有可能,你的getView()方法正确初始化一个新的细胞看法,但永远不会更新已经回收单元视图的状态。所以基本上每个单元视图将只显示它被初始化的状态。它应该是这个样子:

The problem is mostly likely that your getView() method correctly initialises a new cell view but never updates the state of a cell view that has been recycled. So essentially each cell view will only ever display the state it was initialised to. It should look something like this:

public View getView(int position, View convertView, ViewGroup parent) {
    View cell = convertView;

    if (cell == null) {
        // No View passed, create one.
        cell = mInflater.inflate(R.layout.galleryitem, null);
        // Setup the View content
        ImageView imageView = (ImageView)cell.findViewById(R.id.thumbImage);
        CheckBox checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox);
        checkBox.setChecked(checkboxesState[position]);
        checkBox.setId(position);
        imageView.setImageBitmap(downsample(mThumbIds[position]));

        // Setup the View behavior
        imageView.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Uncheck all checked items
                uncheckAll();
            }
        });

        checkBox.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                checkboxesState[cb.getId()] = cb.isChecked();
            }
        });
    } else {
        ImageView imageView = (ImageView)cell.findViewById(R.id.thumbImage);
        CheckBox checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox);
        checkBox.setChecked(checkboxesState[position]);
        checkBox.setId(position);
        imageView.setImageBitmap(downsample(mThumbIds[position]));
    }

    return cell;
}

此外,以提高你的getView()方法的性能来看看这的教程

Additionally to improve the performance of your getView() method take a look at this tutorial.

这篇关于GridView的自定义ImageAdapter数据更改后不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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