gpt4 book ai didi

Android:如何设置选中列表项?

转载 作者:行者123 更新时间:2023-11-29 22:29:13 36 4
gpt4 key购买 nike

我有一个带有复选框的 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);

}

最佳答案

  1. 将行布局中复选框的资源 ID 放入 to数组,对应于NotesDbAdapter.KEY_CHECKED光标在 from数组。

  2. 实现 SimpleCursorAdapter.ViewBinder .

  3. 拥有 ViewBinder.setViewValue()方法检查何时调用 NotesDbAdapter.KEY_CHECKED专栏。

  4. 当它不是 KEY_CHECKED 列时,让它返回 false适配器将执行它通常执行的操作。

  5. 当它是 KEY_CHECKED 列时,让它根据需要将 CheckBox View (需要转换)设置为选中或不选中,然后返回 true这样适配器就不会尝试绑定(bind)它自己。游标和对应的列 id 可用于访问查询数据以确定是否选中复选框。

  6. 通过 setViewBinder() 在您的 SimpleCursorAdapter 中设置您的 ViewBinder

这是我的 ViewBinder 实现之一。它不是用于复选框,而是用于对 TextView 进行一些奇特的格式化,但它应该让您对这种方法有所了解:

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;
}
};

关于Android:如何设置选中列表项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4852828/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com