我想从我的 SQLiteDatabase
数据绑定到的ListView
。我目前使用的是 SimpleCursorAdapter
来填补我的的ListView
。不幸的是这似乎不符合设置复选框的选中属性的工作。
I'm trying to bind data from my SQLiteDatabase
to a ListView
. I'm currently using a SimpleCursorAdapter
to fill in my ListView
. Unfortunately this doesn't seem to work with setting a CheckBox's checked attribute.
这是我现在该怎么办呢;而不是改变复选框的选中状态的适配器填写值的文本参数,所以该值显示该复选框为文本权。
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.
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>
编辑:在数据库中的字段是布尔类型,当然我也试过一个ID分配到检查现场填写值
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.
我不知道你将如何从创建推翻NewView的/ bindView或getView,这取决于你忽略了什么自定义适配器为此预留(ResourceCursorAdapter是很好的)。
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).
好了,所以这里是一个例子。我没有测试,看看它是否会编译,因为我在工作,但一定要你指出正确的方向:
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))));
}
}
}