【问题标题】:Android: Binding data from a database to a CheckBox in a ListView?Android:将数据库中的数据绑定到 ListView 中的 CheckBox?
【发布时间】:2010-12-03 02:35:38
【问题描述】:

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

这就是我现在的做法;适配器将值填充到文本参数中,而不是更改 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 以填充值。

【问题讨论】:

    标签: android listview checkbox


    【解决方案1】:

    您可以设置自定义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;
        }
    });
    

    setViewValue 方法会为您在 SimpleCursorAdapter 构造函数中指定的每一列调用,并为您提供一个操作部分(或全部)视图的好地方。

    【讨论】:

    • 唯一的问题是这在 api 级别 3 (Android 1.5) 中不可用。这在 api 级别 5 中可用。
    • 这项技术对我来说非常有效,可以将图像加载到列表视图中。谢谢。
    • 文档说从 API 级别 1 开始可用。此外,现在定位级别 5 及更高级别应该是相当安全的:developer.android.com/resources/dashboard/…
    【解决方案2】:

    除了创建一个覆盖 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))));
            }
        }
    }
    

    【讨论】:

    • 你能给我举个例子吗,因为我是android编程的新手,还没有见过这样的东西?
    • 没问题,让我挖点东西,我会贴出来的。
    • 谢谢!!有效。似乎没有 getBoolean() 函数 ;-).. 我现在正在使用 cbListCheck.setChecked( (cur.getInt(cur.getColumnIndex(Datenbank.DB_STATE))==0? false:true) );哪个可以解决问题。
    • @Emerald214 查看developer.android.com/reference/android/widget/…。在 bindView 你会调用 cbListClick.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { // 把你的代码放在这里 });
    • 无需重写 newView,超类正是这样做的,它返回您在构造函数中提供的布局的膨胀副本。
    【解决方案3】:

    您可以通过创建自定义 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”语句,以免创建无限递归。

    然后将您的自定义类提供到布局文件中:

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

    应该这样做!

    【讨论】:

    • 我最喜欢这个实现,但是当我尝试它时,我的程序在尝试膨胀我的 CustomCheckBox 对象时崩溃。为什么会这样?
    猜你喜欢
    • 1970-01-01
    • 2018-01-09
    • 2014-05-07
    • 2011-11-13
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    相关资源
    最近更新 更多