CheckBox in ListView being reset when it leaves the screen

后端 未结 2 1661
-上瘾入骨i
-上瘾入骨i 2021-01-27 06:32

I have followed the tutorial here to create a custom ListView that shows items with category headers. I have modified the list_item_entry.xml to put a

2条回答
  •  不要未来只要你来
    2021-01-27 07:36

    you need to maintain a status array of type boolean in your activity, pass that array into your list adapter and while setting the checkbox check status of that position, also you need to update that status array likewise on click event of checkbox. try this you will get the desired output.

    //While Setting the checkbox in adapter
    
         if(bStatus[position]==false)
            {
                 itemSet.chSelectItem.setChecked(false);
            }else if(bStatus[position]==true)
                {
                  itemSet.chSelectItem.setChecked(true);
                }   
    

    In your main Activity

          //initilize Arraylist in main Activity
            boolean[] bStatus;
            bStatus = new boolean[BeanArray.size()];
            Arrays.fill(bStatus, false);
        MyAdapter adapter = new MyAdapter(this, BeanArray, bStatus);
    listView.setAdapter(adapter);
    

提交回复
热议问题