ListView绑定checkbox状态。

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

 

 
 
 
 
 
 
 
 

 

数据库的值在里面全都是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; 
    } 
}); 

 

你可能感兴趣的:(android)