MultiChoiceModeListener.onItemCheckedStateChanged() only executes when item is checked

Juanu picture Juanu · Mar 23, 2013 · Viewed 7.8k times · Source

I'm having a problem. In my main activity, which extends ListActivity and Implements MultiChoiceModeListener, I have the overriden method onItemCheckedStateChanged().

The problem is that this method only gets executed when the new checked state of the itrem is CHECKED. If I uncheck it, it doesn't execute.

I check the item programatically from my DataListAdapter. My item layout contains a CheckBox, that when it's checked, i use the control's onCheckedChanged() to change the list item check state.

Any clues?

Here is my code (relevant code only):

Main Activity:

public class MainActivity extends ListActivity implements MultiChoiceModeListener
{
    @Override
    protected void onCreate (Bundle bundle)
    {
        super.onCreate (bundle);
        // Set our view from the "main" layout resource
        setContentView (R.layout.main);

        this.getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
        this.getListView().setMultiChoiceModeListener(this);

        _dataAdapter = new ServerListAdapter (this);
        this.getListView(). setAdapter(_dataAdapter);
        registerForContextMenu (this.getListView());
    }

    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean isChecked) 
    {
        // Some code that only is executed when the setItemChecked is true on the DataListAdapter.        
    }
}

List Adapter

public class ServerListAdapter extends BaseAdapter 
{
    @Override
    public View getView (final int position, View convertView, ViewGroup parent)
    {
        View view = (convertView == null ?
                _context.getLayoutInflater().inflate(
                R.layout.server_list_item_view,
                parent, 
                false) : convertView);

        final ListView listView = (ListView)parent;
        ((CheckBox)view.findViewById(R.id.chkItemServerSelected)).setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
            @Override
            public void onCheckedChanged(CompoundButton pCompound, boolean arg1) 
            {
                listView.setItemChecked(position, pCompound.isChecked());
            }       
        });

        return view;
    }
}

Answer

Amit Gupta picture Amit Gupta · Mar 23, 2013

Try Like this

I have done with Custom Multiple Choice ListView that will solve your issue. Go through the below link for comple source code.

http://amitandroid.blogspot.in/2013/03/android-custon-single-choice-lsitview.html

This is a single choice listview. You need to change and need to change to multipleChoice

 <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:choiceMode="multipleChoice"
        android:listSelector="#00000000" />

Custom Multiple Choice ListView:-

enter image description here

Steps1)

/**
 * 
 */
package com.custom.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.RelativeLayout;


public class CheckableRelativeLayout extends RelativeLayout implements
        Checkable {

    private boolean isChecked;
    private List<Checkable> checkableViews;

    public CheckableRelativeLayout(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        initialise(attrs);
    }

    public CheckableRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialise(attrs);
    }

    public CheckableRelativeLayout(Context context, int checkableId) {
        super(context);
        initialise(null);
    }

    /*
     * @see android.widget.Checkable#isChecked()
     */
    public boolean isChecked() {
        return isChecked;
    }

    /*
     * @see android.widget.Checkable#setChecked(boolean)
     */
    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
        for (Checkable c : checkableViews) {
            c.setChecked(isChecked);
        }
    }

    /*
     * @see android.widget.Checkable#toggle()
     */
    public void toggle() {
        this.isChecked = !this.isChecked;
        for (Checkable c : checkableViews) {
            c.toggle();
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        final int childCount = this.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            findCheckableChildren(this.getChildAt(i));
        }
    }

    /**
     * Read the custom XML attributes
     */
    private void initialise(AttributeSet attrs) {
        this.isChecked = false;
        this.checkableViews = new ArrayList<Checkable>(5);
    }

    /**
     * Add to our checkable list all the children of the view that implement the
     * interface Checkable
     */
    private void findCheckableChildren(View v) {
        if (v instanceof Checkable) {
            this.checkableViews.add((Checkable) v);
        }

        if (v instanceof ViewGroup) {
            final ViewGroup vg = (ViewGroup) v;
            final int childCount = vg.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                findCheckableChildren(vg.getChildAt(i));
            }
        }
    }
}

Step2)

p

ackage com.custom.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.CheckBox;
public class InertCheckBox extends CheckBox {

    public InertCheckBox(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public InertCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public InertCheckBox(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyShortcut(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onTrackballEvent(MotionEvent event) {
        return false;
    }
}

Step3) listitem.xml

<com.custom.view.InertCheckBox
    android:id="@+id/multiitemCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_margin="5dp"
    android:focusable="false" />

<TextView
    android:id="@+id/singleitemId"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/multiitemCheckBox"
    android:focusable="false"
    android:textSize="14sp" />

Hope this help for you.