Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem with listview which list item contain a checkbox. When i check a box and scroll list, checkbox sometime auto call oncheckedchange and value of checkbox is changed!

Or, when my list has more than 9 or 10 item, then when i checked at item 1, item 8 or 9 is checked???

Anyone can tell me what do i fix this bug?

Thanks in advance!

list_item.xml

<ImageView
    android:layout_alignParentLeft="true"
    android:layout_width="36dip"
    android:layout_height="36dip"
    android:layout_centerVertical="true"
    android:scaleType="fitCenter"
    android:id="@+id/image_view"
    android:src="@drawable/icon" />

<TextView android:layout_toRightOf="@id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5dip"
    android:id="@+id/text_view"
    android:lines="1"
    android:textSize="20sp"
    android:textColor="@color/white" />

<TextView android:layout_toRightOf="@id/image_view"
    android:layout_below="@id/text_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dip"
    android:id="@+id/text_view2"
    android:textSize="14sp"
    android:lines="1"
    android:textColor="@color/white" />          

and here is adapter view:

    public View getView(int position, View convertView, ViewGroup parent) {
        Log.e(TAG, "getView");
        ViewHolder mViewHolder;
        if (convertView == null) {
            Log.e(TAG, "Inflater is inflating...");
            convertView = mInflater.inflate(R.layout.custom_list_app, null);
            mViewHolder = new ViewHolder();
            mViewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
            mViewHolder.remove = convertView.findViewById(R.id.music_info);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
            Log.e(TAG, "Position: " + position + " CheckBox: " + mViewHolder.checkbox.isChecked());
        }   
        mViewHolder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton cb, boolean flag) {
                // TODO Auto-generated method stub
                if (flag) {
                    Log.d(TAG, "Checkbox checked");
                } else {
                    Log.d(TAG, "Checkbox un-checked");
                }
            }
        });


        return convertView;
    }
share|improve this question

1 Answer

up vote 16 down vote accepted

The ListView recycles the view classes: you will need to explicitly set whether or not the CheckBox is checked in the getView class. So add a check like

/**
*    Ensure no other setOnCheckedChangeListener is attached before you manually
*    change its state.
*/
mViewHolder.checkbox.setOnCheckedChangeListener(null);
if(shouldBeChecked) mViewHolder.checkbox.setChecked(true);
else mViewHolder.checkbox.setChecked(false);

before you call setOnCheckedChangeListener.

share|improve this answer
So i think the solution like this. I use an array to store position when checkbox is checked and check where position is checked before call setOnCheckedChangeListener. But when i scroll list(not check) position where is checked has changed! I don't understand why it happen – khaintt May 23 '11 at 17:34
Well, you should ALWAYS call setOnCheckedChangeListener, but you should use the position argument to getView to tell whether or not the checkbox should be checked and change it appropriately EVERY TIME getView is called. – Femi May 23 '11 at 17:38
1  
Let me repeat that: you MUST call either setChecked(true) or setChecked(false) EVERY time getView is called if you are overriding getView. – Femi May 23 '11 at 17:39
Thanks @Femi so much ^^! – khaintt May 23 '11 at 19:26
1  
Femi, has it right. I was missing the call to setting the onchangelistner to null and the check changed was firing automatically. unsetting the listnener before the value is set is critical. – CF_Maintainer May 14 '12 at 16:45
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.