简体   繁体   English

带有复选框问题的Android列表视图

[英]Android listview with checkbox problem

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测试应该是另一种方式!

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. 问题在于滚动真的是因为每次我滚动onCheckedChanged方法都被调用,并且因为它回收了它在新行的位置传递的行,它没有被选中但是因为它与先前选择的那个具有相同的索引它改变了它的价值。 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). 例如,如果我选中带索引2的框(将其设置为true),然后向下滚动一个新行变为索引为2的行,则再次调用该方法并取消设置复选框(已检查数组中的字段)。

I need it to "remember" all the boxes that are checked. 我需要它来“记住”所有被检查的盒子。 In other words I want the checked array to be initialised properly. 换句话说,我希望正确初始化checked数组。 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 . 问题是你正在调用setChecked ,激活旧的OnCheckedChangeListener

The fix is quite simple: call setOnCheckedChangeListener before calling setChecked . 修复非常简单:在调用setChecked之前调用setOnCheckedChangeListener This way you sever the link to the old listener from the recycled view. 这样,您可以从循环视图中切断与旧侦听器的链接。

Check this for Single selection of Checkbox in Listview 选中此选项可在Listview单击选中Checkbox

AdpList adapter=new AdpList(this,array,false);
listview.setAdapter(adapter);

When you select any Item then on item CLickListener do as Follows 当您选择任何项目时,然后在项目CLickListener执行Follows

listview.setonItemClickListener(new OnItemClickListener)

Here You will Get the Position of the Current Item 在这里,您将获得当前项目的位置

array.get(Position);

Get the Checkbox view and On clicking the Checkbox, create a method in Adapter, Like 获取Checkbox视图,单击Checkbox,在Adapter中创建一个方法,Like

adapter.setList(true);
adapter.notifyDataSetChanged();

And In Adapter check the Parameter that I have Passed in the Setlist method then put condition: 并在适配器中检查我在Setlist方法中传递的参数然后放入条件:

if (isChecking==true)
{
checkbox1.setChecked(true);
}else{
checkBox1.setChecked(false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM