Cursor Adapter with multi select
I've written an APP the uses has a small SQL lite DB and using a cursor adapter I can retrieve the records and populate a list view with them. from there I can get the Id of a selected item and delete it from the DB which works great. the issue I have is that as the DB grows deleting one row at a time would be slow and frustrating so I wanted to know if there was any way to allow multiple selections possibly with check boxes or by even changing the text color of the items selected so that I can retreiving their relative ID's.
I have read some posts that talk about custom cursor adapters but I am not sure how to adapt them to my code. I have posted my 开发者_如何学Gocode below.
private void fillData() {
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] {DBHelper.KEY_FIELD0,
DBHelper.KEY_FIELD1,
DBHelper.KEY_FIELD2,
DBHelper.KEY_FIELD3,
DBHelper.KEY_FIELD4};
int[] to = new int[] {R.id.text,R.id.text2,R.id.text3,R.id.text4,R.id.text5};
SimpleCursorAdapter dblist = new SimpleCursorAdapter(this, R.layout.row, c, from, to);
setListAdapter(dblist);
}
Thanks.
I have not done that myself but I did find this tutorial that explains how to add check boxes to a list:
http://www.androidpeople.com/android-listview-multiple-choice-example/
You can use that example to allow multipe selection of items in a list control. Instead of using the array array adaptor as in this example instead use your SimplecursorAdapter
. The only gotcha you will have to work out is that the example uses android.R.layout.simple_list_item_multiple_choice
for the list entries and you need to replace that with your layout since you obviously want a few more than one text field.
I'm not at home so I cannot try it out but I did find this: http://www.mail-archive.com/android-developers@googlegroups.com/msg21920.html
which seems to refer to making your own multiple_choice layout for a list item.
After the user has made their choices you can remove multiple records at one time by running SQL in your database adaptor:
DELETE FROM test WHERE _id IN (1, 3, 6, 7)
where the list of numbers are the selected id values.
精彩评论