代码之家  ›  专栏  ›  技术社区  ›  svens

Android:将数据库中的数据绑定到ListView中的复选框?

  •  43
  • svens  · 技术社区  · 15 年前

    我正在尝试从我的数据库绑定数据 SQLiteDatabase ListView . 我目前正在使用 SimpleCursorAdapter . 不幸的是,这似乎不适用于设置复选框的checked属性。

    我现在就是这样做的;适配器没有更改复选框的选中状态,而是将值填充到文本参数中,因此该值以文本形式显示在复选框的右侧。

    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>
    

    编辑:数据库中的字段当然是boolean类型,我还尝试为选中的字段分配一个id来填充值。

    3 回复  |  直到 12 年前
        1
  •  37
  •   Josef Pfleger    15 年前

    你可以设定一个习惯 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

        2
  •  33
  •   MattC    15 年前

    除了创建一个覆盖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))));
            }
        }
    }
    
        3
  •  0
  •   Nick    15 年前

    您可以通过创建自定义复选框小部件来解决该问题,如下所示:

    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("");
          }
         }
        }
    

    然后在布局文件中提供自定义类,如下所示:

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

    那就够了!