Android的列表视图使用复选框问题 [英] Android listview with checkbox problem

查看:153
本文介绍了Android的列表视图使用复选框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题!我试图创建一个复选框列表视图。在我的其他线程有人告诉我,我应该使用,保持轨道的数组被选中的行。我这样做,它工作得很好十岁上下,但逻辑是错误的,我遇到另外一个问题了。

I have a weird problem! I'm trying to create a listview with checkboxes. In my other thread I was told that I should use an array that keeps track of the rows that are checked. I did that and it worked fine-ish but the logic is wrong and I run into another problem now.

public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }

        CheckBox checkbox = (CheckBox)v.findViewById(R.id.checkbox);
        checkbox.setChecked(checked[position]);

        final LinearLayout rowLayout = (LinearLayout) v.findViewById(R.id.individualRow);

        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {
                    rowLayout.setBackgroundColor(Color.GRAY);                       
                    checked[position] = false;                      
                }
                else
                {   
                    rowLayout.setBackgroundColor(Color.DKGRAY);
                    checked[position] = true;                       
                }
            }               
        });
   }

把所有的复选框取消选中开始正常工作也保留了我选择的检查,即使我向下滚动,再次备份,但检查数组没有正确设置的。基本上,如果测试应该是另一种方式以防万一!

Having all the checkboxes unchecked initially it works fine it keeps the ones that i select checked even if I scroll down and back up again but the checked array is not properly set up. Basically the if test should be the other way arround!

if(isChecked)
{
   rowLayout.setBackgroundColor(Color.GRAY);                        
   checked[position] = true;                        
}
else
{   
   rowLayout.setBackgroundColor(Color.DKGRAY);
   checked[position] = false;                       
}

现在的问题是与卷轴真的,因为我每次滚动onCheckedChanged方法被调用,因为它的回收行它通过在新行的,他不是选择的位置,但由于它具有相同的指数作为一个为previously选择它改变了它的价值。例如,如果我请与指数2(将其设置为true),然后向下滚动新的行变为索引2行,该方法被再次调用,它会清除该复选框(选中数组中的字段)的框。

The problem is with the scroll really because every time I scroll the onCheckedChanged method is called and since its recycling the rows it passes in the position of the new row that its not selected but since it has the same index as the one that was previously selected it changes its value. for example if I check the box with index 2 (set it to true) and then scroll down a new row becomes row with index 2, the method is called again and it unsets the checkbox(the field in the checked array).

我需要它记住所有被选中的复选框。换句话说,我想检查数组被正确初始化。并且还记得检查这箱,而不是失去他们每次我滚动!

I need it to "remember" all the boxes that are checked. In other words I want the checked array to be initialised properly. And also to remember which boxes are checked and not lose them everytime I scroll!

我是什么做错了吗?

你能帮帮我吗?

在此先感谢迈克 -

Thanks in advance -- Mike

推荐答案

这是棘手的。

现在的问题是,你正在调用 setChecked ,激活老 OnCheckedChangeListener

The problem is that you are calling setChecked, activating the old OnCheckedChangeListener.

解决方法是相当简单:拨打 setOnCheckedChangeListener 在调用 setChecked 。这样,你切断从回收的视图链接到老听众。

The fix is quite simple: call setOnCheckedChangeListener before calling setChecked. This way you sever the link to the old listener from the recycled view.

这篇关于Android的列表视图使用复选框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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