ControlAltDelete
ControlAltDelete

Reputation: 3694

Listview Custom Layout Multiple Choice

I would like to create a listview that allows multiple choice. The typical solutions is to get a cursor and use the SimpleCursorAdapter.

        SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
                    R.layout.simple_list_item_multiple_choice, cur2, cols2, views2);

I am able to get this working when using the R.layout.simple_list_item_multiple_choice. I get the checkmarks to work when multiple items are selected.

So I decided to try it with a custom made layout. Here is the XML code for my layout.

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


    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/lookup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceSmall" />


    <TextView
        android:id="@+id/hasphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceSmall" />

    <CheckedTextView
        android:id="@+id/checkedTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>

</LinearLayout>

So here is my issue. The layout is inflated fine using the same code and setting the ChoiceMode to multiple on my listview. The data from the cursor is populated fine.

However, the issue I have is that the checkmarks do not show up as selected (a check in the box) when I click on the item. Is there something I am missing that would not involve creating a custom adapter?


l2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

l2 is my listview.

Upvotes: 0

Views: 6368

Answers (2)

Arkadiusz Cieśliński
Arkadiusz Cieśliński

Reputation: 5336

The problem with "the issue I have is that the checkmarks do not show up as selected" is because of your wrong layout. It should be without LinearLayout:


    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/lookup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceSmall" />


    <TextView
        android:id="@+id/hasphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceSmall" />

    <CheckedTextView
        android:id="@+id/checkedTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:checkMark="?android:attr/listChoiceIndicatorMultiple" />

Then the SimpleCursorAdapter can set properly your "checkedTextView1" (with visible effect).

In my case I used the ArrayAdapter and it worked.


    list.setAdapter(new ArrayAdapter<String>(this, R.layout.category_layout,
                    R.id.category_checkedText, this.categories));

Upvotes: 1

Barak
Barak

Reputation: 16393

I'm not up on CheckedTextView... from my understanding, it should toggle the check when you click the line.

I suppose you could try and force it like this:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {  
    CheckedTextView chkTxt = (CheckedTextView) v.findViewById(R.id.CheckedTextView1); 
    chkTxt.toggle(); 
}

Note this would be a hack and you should really find the root problem, but it might get you going in the meantime.

EDIT

After much googling, I found the issue... the root of the row layout needs to be checkable to use CheckedTextView, and a LinearLayout is not.

More googling found a solution... override the LinearLayout class.

Instructions/code here

Upvotes: 5

Related Questions