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

我正在尝试将数据从我绑定SQLiteDatabase到ListView。我目前正在使用SimpleCursorAdapter来填写我的ListView。不幸的是,这似乎不适用于设置CheckBox的checked属性。


我现在就是这样做的。适配器没有将CheckBox的选中状态更改为文本值,而是将其填写到文本参数中,因此该值以文本形式显示在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>

编辑:数据库中的字段当然是布尔型的,我也尝试过为已检查的字段分配ID以填充值。


ibeautiful
浏览 491回答 3
3回答

慕桂英4014372

除了创建一个覆盖newView / bindView或getView的自定义适配器之外,我不确定您将如何执行此操作,具体取决于您覆盖的内容(ResourceCursorAdapter是个不错的选择)。好的,这是一个例子。我没有测试是否可以编译,因为我正在工作,但这绝对可以为您指明正确的方向:public class MyActivity extends ListActivity {&nbsp; &nbsp; MyAdapter mListAdapter;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; Cursor myCur = null;&nbsp; &nbsp; &nbsp; &nbsp; myCur = do_stuff_here_to_obtain_a_cursor_of_query_results();&nbsp; &nbsp; &nbsp; &nbsp; mListAdapter = new MyAdapter(MyActivity.this, myCur);&nbsp; &nbsp; &nbsp; &nbsp; setListAdapter(mListAdapter);&nbsp; &nbsp; }&nbsp; &nbsp; private class MyAdapter extends ResourceCursorAdapter {&nbsp; &nbsp; &nbsp; &nbsp; public MyAdapter(Context context, Cursor cur) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(context, R.layout.mylist, cur);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public View newView(Context context, Cursor cur, ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return li.inflate(R.layout.mylist, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void bindView(View view, Context context, Cursor cur) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView tvListText = (TextView)view.findViewById(R.id.list_text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.list_checkbox);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvListText.setText(cur.getString(cur.getColumnIndex(Datenbank.DB_NAME)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(Datenbank.DB_STATE))==0? false:true))));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

桃花长相依

您可以设置一个自定义SimpleCursorAdapter.ViewBinder:SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(/* ur stuff */);cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {&nbsp; &nbsp; public boolean setViewValue(View view, Cursor cursor, int columnIndex) {&nbsp; &nbsp; &nbsp; &nbsp; if(columnIndex == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckBox cb = (CheckBox) view;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cb.setChecked(cursor.getInt(1) > 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }});setViewValue在SimpleCursorAdapter构造函数中为您指定的每个列都调用该方法,并为您提供了一个操作某些(或全部)视图的好地方。

阿晨1998

您可以通过创建自定义CheckBox小部件来解决该问题,如下所示:package com.example.CustomCheckBox;&nbsp; &nbsp;&nbsp;public class CustomCheckBox extends CheckBox {&nbsp; &nbsp; &nbsp;public CustomCheckBox(Context context, AttributeSet attrs, int defStyle) {&nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;public CustomCheckBox(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;public CustomCheckBox(Context context) {&nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;protected void onTextChanged(CharSequence text, int start, int before, int after) {&nbsp; &nbsp; &nbsp; if (text.toString().compareTo("") != 0) {&nbsp; &nbsp; &nbsp; &nbsp;setChecked(text.toString().compareTo("1") == 0 ? true : false);&nbsp; &nbsp; &nbsp; &nbsp;setText("");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }当ListView将数据绑定到CheckBox时(即添加“ 0”或“ 1”),将调用onTextChanged函数。这将捕获该更改并添加您的布尔处理。需要第一个“ if”语句,以免产生无限递归。然后像这样在布局文件中提供您的自定义类:<com.example.CustomCheckBox&nbsp;android:id="@+id/rowCheckBox"&nbsp;android:layout_height="fill_parent"&nbsp;android:layout_width="wrap_content" />那应该做!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android