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

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

问题描述

我有一个奇怪的问题!我正在尝试创建一个带有复选框的列表视图.在我的另一个线程中,有人告诉我应该使用一个数组来跟踪检查的行.我这样做了,它运行良好,但逻辑错误,我现在遇到了另一个问题.

public View getView(final int position, View convertView, ViewGroup parent) {视图 v = 转换视图;如果(v == 空){LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);v = vi.inflate(R.layout.row, null);}CheckBox 复选框 = (CheckBox)v.findViewById(R.id.checkbox);checkbox.setChecked(checked[位置]);final LinearLayout rowLayout = (LinearLayout) v.findViewById(R.id.individualRow);checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {如果(已检查){rowLayout.setBackgroundColor(Color.GRAY);已检查[位置] = 假;}别的{rowLayout.setBackgroundColor(Color.DKGRAY);已检查[位置] = 真;}}});}

最初未选中所有复选框它可以正常工作,即使我向下滚动并再次备份但未正确设置选中的数组,它也会保持选中我选择的复选框.基本上 if 测试应该是相反的!

if(isChecked){rowLayout.setBackgroundColor(Color.GRAY);已检查[位置] = 真;}别的{rowLayout.setBackgroundColor(Color.DKGRAY);已检查[位置] = 假;}

问题出在滚动上,因为每次我滚动时都会调用 onCheckedChanged 方法,并且由于它回收了它在未选择的新行的位置传递的行,但因为它具有与未选择的新行相同的索引之前被选中它会改变它的值.例如,如果我选中索引为 2 的框(将其设置为 true),然后向下滚动一个新行成为索引为 2 的行,则再次调用该方法并取消设置复选框(选中数组中的字段).

我需要它来记住"所有选中的框.换句话说,我希望检查的数组正确初始化.还要记住每次滚动时都选中了哪些框并且不会丢失它们!

我做错了什么?

你能帮我吗?

提前致谢——迈克

解决方案

这很棘手.

问题是您正在调用 setChecked,激活旧的 OnCheckedChangeListener.

修复非常简单:在调用 setChecked 之前调用 setOnCheckedChangeListener.这样你就可以从回收的视图中切断到旧侦听器的链接.

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;                       
}

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!

What am I doing wrong?

Can you please help me?

Thanks in advance -- Mike

解决方案

This was tricky.

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

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天全站免登陆