I am trying to implement a very simple "todo" list. I chose to use a ListView of CheckedTextView (with id checked_text), the ListView uses CHOICE_MODE_MULTIPLE of course.
Now, I am using the SQLite database to store the list items.
To fill the list I have something like:
cursor = dbHelper.fetchAll();
startManagingCursor(cursor);
String[] from = new String[] { DbAdapter.NAME };
int[] to = new int[] { R.id.checked_text };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.todo_multi_row, cursor, from, to);
listView.setAdapter(notes);
This is working fine, it gets all the items in the DB and for each item it creates a CheckedTextView with as text DbAdapter.NAME.
But what I really need is to get both the text, and the checked state, and check the Rows accordingly.
Let's say I can get the state (as integer 0/1) from the DB using DbAdapter.CHECKED.. how can I implement a cursor adapter that gets the job done?
I searched everywhere (for instance this question which is a mess) but found nothing to solve this problem. Wah. It seemed so easy at first.
Thanks,
Stefano
@Bert F:
Thanks, this is the method I am using to fill my list:
private void fillData() {
Log.d("LIST","fill");
cursor = dbHelper.fetchAllTodos();
startManagingCursor(cursor);
String[] from = new String[] { TodoDbAdapter.NAME };
int[] to = new int[] { R.id.checked_text };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.todo_multi_row, cursor, from, to);
final SimpleCursorAdapter.ViewBinder mViewBinder =
new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(
final View view,
final Cursor cursor,
final int columnIndex) {
Log.d("LIST",view +" "+cursor+" "+columnIndex);
CheckedTextView item = (CheckedTextView) view;
Log.d("LIST","NAME: "+item.getText()+" State: "+item.isChecked());
return false;
}
};
notes.setViewBinder(mViewBinder);
listView.setAdapter(notes);
}
Now, as a test I am just printing out what is happening.
If I add 1 element to the list what I see is:
02-12 14:45:35.913: DEBUG/LIST(22621): fill
02-12 14:45:35.933: DEBUG/LIST(22621): android.widget.CheckedTextView@479da398 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:35.933: DEBUG/LIST(22621): NAME: State: false
02-12 14:45:35.933: DEBUG/LIST(22621): android.widget.CheckedTextView@479da398 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:35.943: DEBUG/LIST(22621): NAME: first item State: false
02-12 14:45:36.013: DEBUG/LIST(22621): android.widget.CheckedTextView@479dc6c0 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:36.013: DEBUG/LIST(22621): NAME: State: false
02-12 14:45:36.223: DEBUG/LIST(22621): android.widget.CheckedTextView@479dc6c0 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:36.223: DEBUG/LIST(22621): NAME: first item State: false
And if I check it..
02-12 14:53:33.123: DEBUG/LIST(22621): android.widget.CheckedTextView@479dc6c0 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:53:33.123: DEBUG/LIST(22621): NAME: first item State: false
02-12 14:53:33.123: DEBUG/LIST(22621): android.widget.CheckedTextView@479da398 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:53:33.123: DEBUG/LIST(22621): NAME: first item State: false
Let aside the fact that it says it is not checked, why is it firing so many times ? I just have 1 item :I
EDIT: ugly solution
private void fillData() {
Log.d("LIST","fill");
cursor = dbHelper.fetchAllTodos();
startManagingCursor(cursor);
String[] from = new String[] { TodoDbAdapter.NAME };
int[] to = new int[] { R.id.checked_text };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.todo_multi_row, cursor, from, to);
final SimpleCursorAdapter.ViewBinder mViewBinder =
new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(
final View view,
final Cursor cursor,
final int columnIndex) {
final int checkedIndex =
cursor.getColumnIndexOrThrow(
TodoDbAdapter.CHECKED);
Log.d("LIST","VIEW: "+view+" NAME: "+cursor.getString(columnIndex)+" "+cursor.getInt(checkedIndex));
return false;
}
};
notes.setViewBinder(mViewBinder);
listView.setAdapter(notes);
}
With 2 entry (first selected second not) when I load the list I have:
02-12 15:59:48.613: DEBUG/LIST(23533): fill
02-12 15:59:48.643: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a30ec0 NAME: test 1
02-12 15:59:48.653: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a30ec0 NAME: LOL 0
02-12 15:59:48.683: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a30ec0 NAME: test 1
02-12 15:59:48.683: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a331f8 NAME: LOL 0
02-12 15:59:48.713: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: test 1
02-12 15:59:48.713: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: LOL 0
02-12 15:59:48.783: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: test 1
02-12 15:59:48.783: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: LOL 0
Cheers.