Android:ListView 的 CheckedTextView + SQLite,使用适配器显示选中的行?
我正在尝试实现一个非常简单的“待办事项”列表。 我选择使用CheckedTextView的ListView(id为checked_text),ListView当然使用CHOICE_MODE_MULTIPLE。
现在,我使用 SQLite 数据库来存储列表项。
为了填写列表,我有类似的内容:
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);
这工作正常,它获取数据库中的所有项目以及每个项目 它创建一个带有文本 DbAdapter.NAME 的 CheckedTextView。
但我真正需要的是获取文本和选中状态, 并相应地检查行。
假设我可以使用以下命令从数据库获取状态(作为整数 0/1) DbAdapter.CHECKED ..如何实现获取游标适配器 工作完成了吗?
我到处搜索(例如 这个问题 这是一团糟)但没有找到解决这个问题的办法。哇。 起初看起来很容易。
谢谢,
斯特凡诺
@Bert F:
谢谢,这是我用来填写列表的方法:
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);
}
现在,作为测试,我只是打印出正在发生的事情。
如果我向列表中添加 1 个元素,我看到的是:
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
如果我检查它..
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
抛开它说它没有检查的事实,为什么它会触发这么多次?我只有 1 项:我
编辑:丑陋的解决方案,
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);
}
当我加载列表时,有 2 个条目(第一个选择第二个不选择):
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
干杯。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现SimpleCursorAdapter.ViewBinder。
有 ViewBinder.setViewValue() 方法检查何时为
DbAdapter.NAME
列调用,或者让它检查R.id。 check_text
-类型视图。当它调用时,但不是针对目标列/视图,让它返回
false
,以便适配器按照正常方式绑定列/视图当它调用目标列/视图时,让它为
(CheckedTextView) 视图设置文本和复选框
。它最终应该返回true
,以便适配器不会继续尝试绑定视图本身。请注意,游标可用于访问查询数据以确定是否选中复选框 (DbAdapter.CHECKED
)。通过 setViewBinder()
这里是我的 ViewBinder 实现之一。它不是用于复选框,而是用于对文本视图进行一些奇特的格式化,但它应该为您提供一些有关该方法的想法:
Implement a SimpleCursorAdapter.ViewBinder.
Have the ViewBinder.setViewValue() method check for when its called for the
DbAdapter.NAME
column or else have it check for theR.id.checked_text
-type view.When it called, but not for not the target column/view, have it return
false
so that the adapter will do bind the column/view the way it normally would.When it called for the target column/view, have it set the text and checkbox for the
(CheckedTextView) view
. It should end up returningtrue
so that adapter won't continue to attempt to bind the view itself. Note that the cursor is available to access query data to determine whether to check the checkbox or not (DbAdapter.CHECKED
).Set your
ViewBinder
in yourSimpleCursorAdapter
via setViewBinder()Here's one of my ViewBinder implementations. Its not for checboxes, rather its for doing some fancy formatting of a text view, but it should give you some idea for the approach: