Android:从数据库绑定数据到ListView中的checkbox?

我试图将数据从我的SQLiteDatabase绑定到一个ListView 。 我目前正在使用SimpleCursorAdapter来填充我的ListView 。 不幸的是,这似乎并没有设置CheckBox的检查属性。

我现在是这样做的; 不是更改CheckBox的检查状态,而是将适配器的值填充到text参数中,所以该值作为文本显示在CheckBox右侧。

Java的:

 setListAdapter( new SimpleCursorAdapter( this, R.layout.mylist, data, new String[] { Datenbank.DB_STATE, Datenbank.DB_NAME }, new int[] { R.id.list_checkbox, R.id.list_text } ) ); 

mylist.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <CheckBox android:text="" android:id="@+id/list_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" ></CheckBox> <TextView android:text="" android:id="@+id/list_text" android:layout_width="wrap_content" android:layout_height="wrap_content" ></TextView> </LinearLayout> 

编辑:在数据库中的字段当然是布尔types,我也试图分配一个ID到选中的字段来填充的值。

我不知道如何做到这一点,除了创build一个自定义适配器覆盖newView / bindView或getView取决于你重写什么(ResourceCursorAdapter是一个很好的)。

好吧,这是一个例子。 我没有testing,看看它是否会编译,因为我在工作,但这应该明确指出你在正确的方向:

 public class MyActivity extends ListActivity { MyAdapter mListAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Cursor myCur = null; myCur = do_stuff_here_to_obtain_a_cursor_of_query_results(); mListAdapter = new MyAdapter(MyActivity.this, myCur); setListAdapter(mListAdapter); } private class MyAdapter extends ResourceCursorAdapter { public MyAdapter(Context context, Cursor cur) { super(context, R.layout.mylist, cur); } @Override public View newView(Context context, Cursor cur, ViewGroup parent) { LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); return li.inflate(R.layout.mylist, parent, false); } @Override public void bindView(View view, Context context, Cursor cur) { TextView tvListText = (TextView)view.findViewById(R.id.list_text); CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.list_checkbox); tvListText.setText(cur.getString(cur.getColumnIndex(Datenbank.DB_NAME))); cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(Datenbank.DB_STATE))==0? false:true)))); } } } 

你可以设置一个自定义的SimpleCursorAdapter.ViewBinder

 SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(/* ur stuff */); cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { public boolean setViewValue(View view, Cursor cursor, int columnIndex) { if(columnIndex == 1) { CheckBox cb = (CheckBox) view; cb.setChecked(cursor.getInt(1) > 0); return true; } return false; } }); 

对于在SimpleCursorAdapter构造函数中指定的每一列,都会调用setViewValue方法,并为您提供了一个处理某些(或全部)视图的好地方。

你可以通过创build一个自定义的CheckBox小部件来解决这个问题:

 package com.example.CustomCheckBox; public class CustomCheckBox extends CheckBox { public CustomCheckBox(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public CustomCheckBox(Context context, AttributeSet attrs) { super(context, attrs); } public CustomCheckBox(Context context) { super(context); } protected void onTextChanged(CharSequence text, int start, int before, int after) { if (text.toString().compareTo("") != 0) { setChecked(text.toString().compareTo("1") == 0 ? true : false); setText(""); } } } 

当ListView将数据绑定到checkbox(即添加“0”或“1”)时,onTextChanged函数将被调用。 这将捕捉到这个改变,并添加你的布尔处理。 需要第一个“if”语句以便不创build无限recursion。

然后将您的自定义类提供给布局文件,如下所示:

 <com.example.CustomCheckBox android:id="@+id/rowCheckBox" android:layout_height="fill_parent" android:layout_width="wrap_content" /> 

这应该做到这一点!