在listView中使用checkBox,checkBox不会有作用如:
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 } ) );
<?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>
数据库的值在里面全都是fouse,而数据库中本身的状态如果是true,但是这个按钮还是false。
那么现在你可以这么用:
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)))); } } }
或者
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; } });