I have a list view with a Checkbox in every line to check many items at a time. When I scroll down the list my checkbox looses the state (that means, I cant check for example all items at a time)

I read lots of posts here and on the internet. People said that I would have to store my checkedItems in an 'ArrayList` which I already tried but it didn't work. At the moment I store the checked state in an Object together with all the other stuff of my list.

here's the code of my Adapter`:

    public class GalleryListAdapter extends BaseAdapter {
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.gallery_list_item, parent, false);
            holder = new ViewHolder();
            // .. other stuff
            holder.chkbox = (CheckBox) convertView.findViewById(R.id.chkbox);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.chkbox.setChecked(documents.get(position).isChecked());
        holder.chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    holder.chkbox.setChecked(holder.chkbox.isChecked() ? true : false);
                    documents.get(position).setChecked(holder.chkbox.isChecked() ? true : false);
            }   
        }
    }
}

After I scroll down the list my items are unchecked and documents.get(checkedItemPosition).checked is false. Does anyone know how to fix this?

edit - Solution

Thanks to the comments and answers I found a solution for my problem:

if (convertView == null) {
holder.chkbox = (CheckBox) convertView.findViewById(R.id.chkbox);
convertView.setTag(R.id.chkbox, holder.chkbox);
}

holder.chkbox.setTag(position);
holder.chkbox.setChecked(documents.get(position).isChecked());

holder.chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int getPosition = (Integer) buttonView.getTag();
        holder.chkbox.setChecked(buttonView.isChecked() ? true : false);
        documents.get(getPosition).setChecked(buttonView.isChecked() ? true : false);
    }
}
有帮助吗?

解决方案

in getView method set Tag for the checkbox holder.

holder.chkbox.setTag(object);

and inside onCheckedChanged do this

 **public onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

           Object obj = ( Object)buttonView.getTag();
           holder.chkbox.setChecked(obj.isChecked() ? true : false);
           documents.get(position).setChecked(obj.isChecked() ? true : false);
      }** 

This will help you get rid of that problem ;)

其他提示

When I scroll down the list my checkbox looses the state

The issue with CheckBox inside ListView is that the view gets recycled due to scrolling of the ListView and the value of Checkbox(check or uncheck) is not maintained.

To, maintain the state to CheckBox i suggest you to use TAG for the checkbox.

holder.chkbox.setTag(position); // This line is important.

This is because the ListView hides the child that is not in view when you scroll away, this would cause the state of the CheckBox to be reset. If you do not require your checkbox to be structured inside a table maybe you can consider using ScrollView instead.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top