0

我的任务是创建一个基于. ListActivity数据内容来自数据库SimpleCursorAdapter

独立于光标列,它必须CheckBox在每个列表项中显示一个。用户应该检查任意多的行。当用户完成他的选择时,我必须得到所有检查的行和它的 _id 列,所以我必须知道哪些行被检查了。

怎么做?

没有CheckBoxesListview功能已经可以正常工作。

4

1 回答 1

1

One quick way would be to use android.R.layout.simple_list_item_multiple_choice. This code example does what you want (just note that I used Contacts for my Cursor):

public class ListCheck extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_check);

        this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        startManagingCursor(cursor);

        String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME };
        int[] to = new int[] { android.R.id.text1 };

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_multiple_choice, cursor, columns, to);
        this.setListAdapter(adapter);

        Button finishButton = (Button) this.findViewById(R.id.finishButton);
        finishButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                SimpleCursorAdapter adapter = (SimpleCursorAdapter) ListCheck.this.getListAdapter();
                Cursor cursor = adapter.getCursor();

                ListView lv = ListCheck.this.getListView();
                SparseBooleanArray selectedItems = lv.getCheckedItemPositions();
                for (int i = 0; i < selectedItems.size(); i++) {

                    int selectedPosition = selectedItems.keyAt(i);
                    cursor.moveToPosition(selectedPosition);
                    Log.d("", cursor.getString(cursor.getColumnIndex(
                            ContactsContract.Contacts.DISPLAY_NAME))+" is checked");
                    Log.d("", "row id: "+adapter.getItemId(selectedPosition));
                }
            }
        });
    }
}

Activity layout:

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

    <Button
        android:id="@+id/finishButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="multipleChoice" />

</LinearLayout>

However, if you want to add other custom data inside each row (image, subtitle..), then you have to create a custom layout for your rows using CheckedTextView. CheckedTextView is really the key here, since it's used by the android.R.layout.simple_list_item_multiple_choice.

Just follow this example:

<com.mfp.tests.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <CheckedTextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="added a data: subtitle"/>

</com.mfp.tests.CheckableLinearLayout>

One last thing: I used a custom layout instead of a LinearLayout. In order to make the checkBox of CheckedTextView responsive (automatically checked or unchecked when you click on a row), the layout must implement Checkable (and LinearLayout doesn't), so you'll have to copy the CheckableLinearLayout class from this link:

PS: If you want to try the code above, don't forget to put <uses-permission android:name="android.permission.READ_CONTACTS" /> inside your Manifest.xml

于 2012-08-19T20:21:31.353 回答