Android的:如何设置列表项检查? [英] Android: How to set List item checked?
问题描述
我有一个android.R.layout.simple_list_item_multiple_choice与复选框的要那么启动其中的一些。
我该怎么办呢?
我有以下的code:
私人无效fillList(){
光标NotesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(NotesCursor); 的String [] =由新的String [] {NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY,NotesDbAdapter.KEY_CHECKED}; INT []为= INT新[] {
android.R.id.text1,
android.R.id.text2,
//设置如何检查或检查不?
}; SimpleCursorAdapter指出=新SimpleCursorAdapter(这一点,android.R.layout.simple_list_item_multiple_choice,NotesCursor,
从到);
setListAdapter(注);}
-
把你的复选框的资源ID在你排布置到
到
数组,对应NotesDbAdapter.KEY_CHECKED
光标从
阵列。 -
有<一个href=\"http://developer.android.com/reference/android/widget/SimpleCursorAdapter.ViewBinder.html#setViewValue%28android.view.View,%20android.database.Cursor,%20int%29\"相对=nofollow> ViewBinder.setViewValue()方法的支票时,其所谓的
NotesDbAdapter.KEY_CHECKED
列。 -
在它的不的的KEY_CHECKED列,使其返回
假
的适配器将做什么它一般不会。 -
当它是KEY_CHECKED列,有它设置的复选框视图(投要求)检查或没有如你所愿,然后返回
真正
使适配器不会尝试绑定本身。光标和相应的列ID可访问的查询数据,以确定是否选中复选框或没有。 -
通过<一个在你的SimpleCursorAdapter设置您ViewBinder href=\"http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#setViewBinder%28android.widget.SimpleCursorAdapter.ViewBinder%29\"相对=nofollow> setViewBinder()
下面是我ViewBinder实现之一。它不是checboxes,而它做一个文本视图的一些奇特的格式,但它应该给你一些想法的方式:
私人最终SimpleCursorAdapter.ViewBinder mViewBinder =
新SimpleCursorAdapter.ViewBinder(){
@覆盖
公共布尔setViewValue(
最终视图来看,
最后光标光标,
最终诠释参数:columnIndex){
最终诠释latitudeColumnIndex =
cursor.getColumnIndexOrThrow(
LocationDbAdapter.KEY_LATITUDE);
最终诠释addressStreet1ColumnIndex =
cursor.getColumnIndexOrThrow(
LocationDbAdapter.KEY_ADDRESS_STREET1); 如果(参数:columnIndex == latitudeColumnIndex){ 最后字符串文本= formatCoordinates(光标);
((的TextView)视图).setText(文本);
返回true; }否则如果(参数:columnIndex == addressStreet1ColumnIndex){ 最后字符串文本= formatAddress(光标);
((的TextView)视图).setText(文本);
返回true; } 返回false;
}
};
I have an android.R.layout.simple_list_item_multiple_choice with checkboxes an want so initiate some of them. How can I do that? I have the following code:
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);
}
Put the resource id of your checkbox in your row layout into the
to
array, corresponding to theNotesDbAdapter.KEY_CHECKED
cursor infrom
array.Implement a SimpleCursorAdapter.ViewBinder.
Have the ViewBinder.setViewValue() method check for when its called for the
NotesDbAdapter.KEY_CHECKED
column.When it is not the KEY_CHECKED column, have it return
false
the the adapter will do what it normally does.When it is the KEY_CHECKED column, have it set the CheckBox view (cast required) to checked or not as you wish and then return
true
so that adapter won't attempt to bind it itself. The cursor and corresponding column id is available to access query data to determine whether to check the checkbox or not.Set your ViewBinder in your SimpleCursorAdapter 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:
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的:如何设置列表项检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!