0

Listview with checkbox and CustomCursorAdapter

In ListActivity.java

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.list);

    ListView listView = (ListView) findViewById(R.id.lists);
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    //create Cursor called cursor
    listAdapter = new DetailListAdapter(this, cursor);
    getLoaderManager().initLoader(LOADER_ID, null, this);
    listView.setAdapter(listAdapter);
    listView.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3)
        {
            //want to call startactionmode with class implementing ListView.MultiChoiceModeListener
        }

    });

}

In DetailListAdapter.java

public class DetailListAdapter extends CursorAdapter 
{
    private Context         context;
    private ArrayList<Boolean> checked = new ArrayList<Boolean>();
    private LayoutInflater  mLayoutInflater;


    public DetailListAdapter(Context context, Cursor c)
    {
        super(context, c);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        View v = mLayoutInflater.inflate(R.layout.list_item, parent, false);
        return v;
    }

    @Override
    public void bindView(View v, Context context, Cursor c)
    {
        final View view = v;
        final int position = c.getInt(0);
        String data = c.getString(c.getColumnIndexOrThrow("DATA"));

        /**
         * Next set the name of the entry.
         */
        TextView list_text = (TextView) view.findViewById(R.id.listitemtext);

        CheckBox checkbox_text = (CheckBox) view.findViewById(R.id.listitemcheckbox);

        list_text.setText(data);

        checked.add(position - 1, false);
        checkbox_text.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    view.setBackgroundColor(Color.GRAY);
                    checked.set(position - 1, true);
                } else
                {
                    view.setBackgroundColor(Color.BLACK);
                    checked.set(position - 1, false);
                }
            }
        });

        checkbox_text.setChecked(!checkbox_text.isChecked());
    }
}

The list row xml is,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:descendantFocusability="blocksDescendants">

<CheckBox
    android:id="@+id/check1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:focusable="false"
  />

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/check1"
    android:layout_toRightOf="@+id/check1"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" 
   />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/text1"
    android:layout_alignBottom="@+id/text1"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/text1"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" 
   />

<TextView
    android:id="@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/text2"
    android:layout_below="@+id/text1"
    android:layout_toRightOf="@+id/check1"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall" 
   />

 </RelativeLayout>

When I click on list item in emulator, checkbox is checked and row turns into gray. But onitemclick is not getting called. I do have android:focusable="false" and android:focusableInTouchMode="false" on all items in layout which creates a row in list.

anonymous
  • 343
  • 2
  • 14
  • Do you want both `onCheckedChanged()` and `onItemClick()` to fire with one click? – Sam Nov 16 '12 at 17:41
  • 1
    I had similar issue and I just added android:focusable="false" for only the checkbox item in the xml and it worked neatly for me... Try it out. – Abhishek Sabbarwal Nov 16 '12 at 17:41
  • yes when I click list item, I want checkbox to be checked and menu to be displayed at top containing actions that can be perfomed on that selected list item.. – anonymous Nov 16 '12 at 17:46

1 Answers1

1

I want checkbox to be checked and menu to be displayed at top containing actions that can be perfomed on that selected list item..

Two OnClickListeners cannot be called from one touch event. As you see, the first listener's onCheckedChanged() method consumes the event preventing it from reaching the onItemClick() method.

I recommend using a Checkable layout in your rows that will maintain the check state for you and changing your adapter's getView() to set the appropriate background color on account of the view recycler.

(Post your XML in you question if you need help with this.)

Sam
  • 86,580
  • 20
  • 181
  • 179
  • I added list row xml in question – anonymous Nov 16 '12 at 18:18
  • Ok, you are using more than one TextView so the 30 second answer isn't an option. However [this answer](http://stackoverflow.com/a/12125857/1267661) details how to create a custom Checkable layout. Create your own CheckableRelativeLayout and let me know how it goes. – Sam Nov 16 '12 at 18:27
  • Thanks for your help. I implemented CheckableRelativeLayout and it seems to be working fine accept 1 issue. I set choice mode for list to be multiple choice modal but on interface I am able to select only one list row at a time. When I select another row, previous one gets unchecked/deselect. – anonymous Nov 16 '12 at 19:40
  • What happens if you just use `CHOICE_MODE_MULTIPLE`? – Sam Nov 16 '12 at 19:47