简体   繁体   English

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

[英]Android: Binding data from a database to a CheckBox in a ListView?

I'm trying to bind data from my SQLiteDatabase to a ListView . 我正在尝试将SQLiteDatabase中的数据绑定到ListView I'm currently using a SimpleCursorAdapter to fill in my ListView . 我目前正在使用SimpleCursorAdapter来填充我的ListView Unfortunately this doesn't seem to work with setting a CheckBox's checked attribute. 不幸的是,这似乎不适用于设置CheckBox的checked属性。

This is how I do it now; 这就是我现在这样做的方式; instead of changing the CheckBox's checked status the adapter is filling in the value to the text argument, so the value is displayed right of the CheckBox as text. 而不是更改CheckBox的检查状态,适配器正在填充text参数的值,因此该值显示在CheckBox右侧作为文本。

Java: 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: 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>

Edit: The field in the database is of course of type boolean and I've also tried to assign an id to the checked field to fill the value in. 编辑:数据库中的字段当然是boolean类型的,我也尝试将id分配给checked字段以填充值。

You could set a custom SimpleCursorAdapter.ViewBinder : 您可以设置自定义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;
    }
});

The setViewValue method is invoked for every column you specify in the SimpleCursorAdapter constructor and gives you a good place to manipulate some (or all) of the views. 为您在SimpleCursorAdapter构造函数中指定的每个列调用setViewValue方法,并为您提供操作某些(或所有)视图的好地方。

I'm not sure how you would do this aside from creating a custom Adapter that overrode newView/bindView or getView, depending on what you override (ResourceCursorAdapter is a good one). 除了创建一个覆盖newView / bindView或getView的自定义适配器之外,我不知道你会怎么做,这取决于你覆盖的内容(ResourceCursorAdapter是一个很好的)。

Ok, so here's an example. 好的,这是一个例子。 I didn't test to see if it would compile because I'm at work, but this should definitely point you in the right direction: 我没有测试,看看它是否会编译,因为我在工作,但这绝对应该指向你在正确的方向:

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

You can solve that problem by creating a custom CheckBox widget like so: 您可以通过创建自定义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("");
      }
     }
    }

The onTextChanged function will be called when the ListView binds the data to the CheckBox (ie. Adding either "0" or "1"). 当ListView将数据绑定到CheckBox时(即添加“0”或“1”),将调用onTextChanged函数。 This will catch that change and add in your boolean processing. 这将捕获该更改并添加您的布尔处理。 The 1st "if" statement is needed so as to not create infinite recursion. 需要第一个“if”语句,以便不创建无限递归。

Then provide your custom class in to the layout file as such: 然后将您的自定义类提供到布局文件中:

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

That should do it! 应该这样做!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM