Checking a checkbox in listview makes other random checkboxes checked too

后端 未结 3 1322
耶瑟儿~
耶瑟儿~ 2020-11-28 09:14

whenever i check a checkbox in my listview , other random checkboxes get checked too . It could be due to item recycling by listview.

I also tried setting android:f

3条回答
  •  隐瞒了意图╮
    2020-11-28 10:00

    When a listview recycles views , it recycles its present state as well as listeners attached to it. In my example, if the checkbox was checked and has a onCheckedChangeListener set, both will remain a part of recycled view based on position. So it is our responsibility to reset all states and remove previous listeners.

    So when I was unchecking the recycled view, the onCheckedChange listener was getting executed. one line made the program work perfectly. The listener was removed by :

    holder.ckbox.setOnCheckedChangeListener(null); 
    

    Below is the working code of Adapter for people who may stumble upon this problem:

    public class MyCustomAdapter extends ArrayAdapter  {
    
    private List appInfoList;
    private LayoutInflater mInflater;
    private PackageManager pm;
    ArrayList positionArray;
    private Context ctx;
    int[] visiblePosArray;
    private volatile int positionCheck; 
    
    public MyCustomAdapter(Context context, List myList) {
        super(context, NO_SELECTION);
        appInfoList = myList;
        ctx=context;
        mInflater =     (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        pm = context.getPackageManager();
    
        positionArray = new ArrayList(myList.size());
        for(int i =0;i

    }

提交回复
热议问题