Android Checkbox in ListView Adapter getting unchecked after scrolling

627 views Asked by At

I have a custom ListViewAdapter with ImageView, TextView and CheckBox.

When I select particular checkBox and scroll down, after scrolling back, checkBox gets unchecked.
I tried few answers but nothing seems to work.

Here is my code:

public class ListViewAdapterDrawer extends ArrayAdapter<String> {

    ImageView imageViewSensor;
    TextView textViewSensor, textViewLogging;
    CheckBox checkBoxSensor;
    private boolean[] itemChecked;
    int count;

    public ListViewAdapterDrawer(Context context, String[] sensorArray) {
        super(context, R.layout.adapter_listview_drawer, sensorArray);
        itemChecked = new boolean[sensorArray.length];
        for (int i = 0; i < this.getCount(); i++) {
            itemChecked[i] = false;
        }
    }  

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());

            convertView = inflater.inflate(R.layout.adapter_listview_drawer, parent, false);    
        }
        imageViewSensor = (ImageView) convertView.findViewById(R.id.imageViewSensor);
        textViewSensor = (TextView) convertView.findViewById(R.id.textViewSensor);
        checkBoxSensor = (CheckBox) convertView.findViewById(R.id.checkBoxSensor);

        checkBoxSensor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBoxSensor.isChecked()){
                    itemChecked[position] = true;
                }else {
                    itemChecked[position] = false;
                }
            }
        });

        checkBoxSensor.setChecked(itemChecked[position]);               
        return convertView; 
    }   
}
2

There are 2 answers

0
almightyGOSU On

If you read this link regarding how ListView gets recycled, i.e. the recycling mechanism, you should be able to understand why your checkboxes get unselected.

This gives a rather detailed explanation about ListView.

Key Points:

  • ListView recycles non-visible views—called “ScrapViews” in Android’s source code, as you pan around.
  • ListView uses the view recycler to keep adding recycled views below or above the current viewport and moving active views to a recyclable pool as they move off-screen while scrolling.
0
SAM On

Try to use hashmap for maintaining the state of checkbox because the listview recycles the same view when the listview is scroll

  HashMap<Integer, CoreQuestion.SubSection> selectionMap = new HashMap<>();

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());

            convertView = inflater.inflate(R.layout.adapter_listview_drawer, parent, false);    
        }
        imageViewSensor = (ImageView) convertView.findViewById(R.id.imageViewSensor);
        textViewSensor = (TextView) convertView.findViewById(R.id.textViewSensor);
        checkBoxSensor = (CheckBox) convertView.findViewById(R.id.checkBoxSensor);

        checkBoxSensor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBoxSensor.isChecked()){
                    itemChecked[position] = true;
                     selectionMap.put(position,"true")
                }else {
                    itemChecked[position] = false;
                     selectionMap.put(position,"false")
                }

            }
        });
       if(selectionMap.get(posotion)!=null)
       {
              checkBoxSensor.setChecked(itemChecked[position]);

       }             
        return convertView; 
    }   
}

may this help.