views:

1121

answers:

1

I've seen example com.example.android.apis.view.List11 from ApiDemos. In that example, each row takes the view android.R.simple_list_item_multiple_choice. Each such view has a TextView and a CheckBox.

Now I want each view to have 2 TextView's and 1 CheckBox, somewhat similar to the List3 example. I tried creating a custom layout file row.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />
    <TextView
        android:id="@+id/text_name"
        android:textSize="13px"
        android:textStyle="bold"
        android:layout_toLeftOf="@id/checkbox"
        android:layout_alignParentLeft="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text_phone"
        android:textSize="9px"
        android:layout_toLeftOf="@id/checkbox"
        android:layout_below="@id/text_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" /> 
</RelativeLayout>

Then in Activity.onCreate(), I do like this:

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

    // Query the contacts
    mCursor = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
    startManagingCursor(mCursor);

    ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.row,
            mCursor, 
            new String[] { Phones.NAME, Phones.NUMBER}, 
            new int[] { R.id.text_name, R.id.text_phone });
    setListAdapter(adapter);
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

The result kind of looks like what I want, but it looks like the list doesn't know which item of it is selected. Also, I need to click exactly on the checkbox. In the List11 example, I only need to click on the item row.

So what do I need to do to make a multiple choice list with my custom view for each row? Many thanks.

+1  A: 

Each such view has a TextView and a CheckBox.

No, it doesn't. It has a CheckedTextView.

So what do I need to do to make a multiple choice list with my custom view for each row?

Try making the CheckBox android:id value be "@android:id/text1" and see if that helps. That is the ID used by Android for the CheckedTextView in simple_list_item_multiple_choice.

CommonsWare
Thanks CommonsWare. I just tried that and that didn't work for me. Btw, where do I look for the implementation of android.R.simple_list_item_multiple_choice? I can't find the xml file with that name in ApiDemos.
Po
It's not in ApiDemos, it's in the SDK. All of the SDK resources reside in `$ANDROID_SDK/platforms/$VERSION/data/res`, where `$ANDROID_SDK` is wherever you installed the SDK and `$VERSION` is some Android version (e.g., `android-2.1`).
CommonsWare
Thanks. I've checked out the content of simple_list_item_multiple_choice. What I found out is it works if the View for each row is just <CheckedTextView>. Once I wrap around it with a Layout, things won't work anymore. I don't know how to cram in one more view to display another piece of text.
Po
Ah. Well, then you are probably stuck managing it all yourself. Here is an example of a list that has a RatingBar and a TextView, with the state of the RatingBar maintained as you scroll: http://github.com/commonsguy/cw-android/tree/master/FancyLists/RateList/ -- the same technique can be adapted for your own checkbox.
CommonsWare