I have simple list to be filled in from the database along with a checkbox. I need a handle to all the checkboxes selected. Whn the CLEAR button is pressed at that point I need the row ids of all the selected check boxes to delete them. To do this : My list.xml file looks like this :
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@id开发者_JAVA百科/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clearselected"
android:text="CLEAR"
android:clickable ="false"/>
</LinearLayout>
and my data_entry.xml looks like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50px"
android:layout_y="22px"></CheckBox>
<TextView android:text="@+id/EntryText"
android:id="@+id/EntryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/blue"/>
</LinearLayout>
Now: I have list.java file where I am populating the list as follows:
private void populateList() {
Cursor c = db1.getAllList();
String[] fields = new String[]{db1.get_data()};
SimpleCursorAdapter cursorAdapter = new
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.data_entry, c,
fields, new int[] {R.id.EntryText});
setListAdapter(adapter);
}
Now where do i give the handle to the heckbox cause anywhere else it would give me a null exception as the data_entry contains the checkboxes. Plus I need a listener to handle the checkbox status? I am just stuck at this point with no clue..
Rather than rolling your own, why not just use a standard ListView with CHOICE_MODE_MULTIPLE enabled?
listView.setChoiceMode(CHOICE_MODE_MULTIPLE);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, fields));
You can then ask the listView for the checked items.
listView.getCheckedItemPositions();
精彩评论