CheckBox gets unchecked on scroll in a custom listview

Dinesh picture Dinesh · Feb 16, 2012 · Viewed 19.7k times · Source

I know that this question has been asked over and over again but still I've not been a able to find a suggestion that really helps me. The checkbox is unchecked whenever the list is scrolled down. Yes I'm using a boolean array to store the values but this still doesn't fix the problem. Here is my code. Please suggest a solution for this. Thank you.

 public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
        final ViewHolder holder;
final boolean[] itemChecked=new boolean[30];

    LayoutInflater inflater =  context.getLayoutInflater();  
    if(convertView==null)  
    {  
        convertView = inflater.inflate(R.layout.custom_list, null);
        holder = new ViewHolder();  
        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text);  
        holder.txtViewDescription = (TextView) convertView.findViewById(R.id.description_text);  
        holder.cb=(CheckBox) convertView.findViewById(R.id.cb);
        convertView.setTag(holder);  

    }  

    else  
    {
        holder=(ViewHolder)convertView.getTag();  

     }  

    holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                 itemChecked[position] = isChecked;
                 if(itemChecked[position])
                 {
                     holder.cb.setChecked(true);
                 }
                 else
                 {
                     holder.cb.setChecked(false);
                 }
      holder.txtViewTitle.setText(title[position]);  
    holder.txtViewDescription.setText(description[position]);  
    holder.cb.setChecked(itemChecked[position]);
  holder.txtViewDescription.setFocusable(false);
  holder.txtViewTitle.setFocusable(false);

return convertView;  

}  

}

Answer

zapl picture zapl · Feb 16, 2012

getView() is called whenever a previously invisible list item needs to be drawn. Since you recreate itemChecked[] each time this method is called you will have the new checkbox unchecked and a different Array for each resulting View. (final in Java does not make that field unique like in C) Simplest way to solve that is to make itemChecked a classmember and set / restore checkbox state based on that one.

public class MyListAdapter extends ArrayAdapter<Object> {
    private final boolean[] mCheckedState;
    private final Context mContext;

    public MyListAdapter(Context context, int resource, int textViewResourceId, List<Object> objects) {
        super(context, resource, textViewResourceId, objects);
        mCheckedState = new boolean[objects.size()];
        mContext = context;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // simplified to just a Checkbox
        // ViewHolder and OnCheckedChangeListener stuff left out 
        CheckBox result = (CheckBox)convertView;
        if (result == null) {
            result = new CheckBox(mContext);
        }
        result.setChecked(mCheckedState[position]);
        return result;
    }
}