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

sve*_*ens 43 checkbox android listview cursor

我正在尝试将数据从我绑定SQLiteDatabaseListView.我现在用的SimpleCursorAdapter是填写我的ListView.不幸的是,这似乎不适用于设置CheckBox的checked属性.

这就是我现在这样做的方式; 而不是更改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 }
    ) );
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

编辑:数据库中的字段当然是boolean类型的,我也尝试将id分配给checked字段以填充值.

Jos*_*ger 37

您可以设置自定义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;
    }
});
Run Code Online (Sandbox Code Playgroud)

setViewValue为您在SimpleCursorAdapter构造函数中指定的每个列调用该方法,并为您提供操作某些(或所有)视图的好地方.

  • 文档说自API级别1以来可用.此外,它现在应该是非常安全的目标5级及以上:http://developer.android.com/resources/dashboard/platform-versions.html (4认同)
  • 唯一的问题是这在api level 3(Android 1.5)中不可用.这可以从api级别5获得. (3认同)

Mat*_*ttC 33

除了创建一个覆盖newView/bindView或getView的自定义适配器之外,我不知道你会怎么做,这取决于你覆盖的内容(ResourceCursorAdapter是一个很好的).

好的,这是一个例子.我没有测试是否会编译,因为我在工作,但这绝对应该指向正确的方向:

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))));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不需要覆盖newView,超类就是这样,它返回你在构造函数中提供的布局的膨胀副本. (3认同)
  • @ Emerald214查看http://developer.android.com/reference/android/widget/CompoundButton.html#setOnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener).在bindView中你要调用cbListClick.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){//把你的代码放在这里}); (2认同)