我有一个带有复选框的android.R.layout.simple_list_item_multiple_choice,因此需要启动其中一些。 我怎样才能做到这一点? 我有以下代码:
private void fillList() {
Cursor NotesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(NotesCursor);
String[] from = new String[] { NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_BODY, NotesDbAdapter.KEY_CHECKED };
int[] to = new int[] {
android.R.id.text1,
android.R.id.text2,
//How set checked or not checked?
};
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, NotesCursor,
from, to);
setListAdapter(notes);
}
答案 0 :(得分:2)
将行格式中复选框的资源ID放入to
数组中,对应NotesDbAdapter.KEY_CHECKED
数组中的from
光标。
让ViewBinder.setViewValue()方法检查其调用NotesDbAdapter.KEY_CHECKED
列的时间。
当不 KEY_CHECKED列时,让它返回false
,适配器将按照正常情况执行。
当它是KEY_CHECKED列时,让它根据需要设置CheckBox视图(需要强制转换),然后返回true
,以便适配器不会尝试自己绑定它。光标和相应的列ID可用于访问查询数据以确定是否选中复选框。
这是我的一个ViewBinder实现。它不是用于checboxes,而是用于对文本视图进行一些奇特的格式化,但它应该让你对这种方法有所了解:
private final SimpleCursorAdapter.ViewBinder mViewBinder =
new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(
final View view,
final Cursor cursor,
final int columnIndex) {
final int latitudeColumnIndex =
cursor.getColumnIndexOrThrow(
LocationDbAdapter.KEY_LATITUDE);
final int addressStreet1ColumnIndex =
cursor.getColumnIndexOrThrow(
LocationDbAdapter.KEY_ADDRESS_STREET1);
if (columnIndex == latitudeColumnIndex) {
final String text = formatCoordinates(cursor);
((TextView) view).setText(text);
return true;
} else if (columnIndex == addressStreet1ColumnIndex) {
final String text = formatAddress(cursor);
((TextView) view).setText(text);
return true;
}
return false;
}
};