Skip to content Skip to sidebar Skip to footer

Checkable Listview Cursor-data Independent Checkbox

My task is to create a ListView based on ListActivity. The data content comes from a database SimpleCursorAdapter. Independent from cursor columns it have to show a CheckBox in ea

Solution 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):

publicclassListCheckextendsListActivity {

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_check);

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

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

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

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

        ButtonfinishButton= (Button) this.findViewById(R.id.finishButton);
        finishButton.setOnClickListener(newOnClickListener() {

            @OverridepublicvoidonClick(View v) {

                SimpleCursorAdapteradapter= (SimpleCursorAdapter) ListCheck.this.getListAdapter();
                Cursorcursor= adapter.getCursor();

                ListViewlv= ListCheck.this.getListView();
                SparseBooleanArrayselectedItems= lv.getCheckedItemPositions();
                for (inti=0; i < selectedItems.size(); i++) {

                    intselectedPosition= 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"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/finishButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button" /><ListViewandroid: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.CheckableLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><CheckedTextViewandroid:id="@+id/text"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    /><TextViewandroid: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

Post a Comment for "Checkable Listview Cursor-data Independent Checkbox"