ListView from cursor with checkbox

I have a Listview that looks like this:

: textview {0 .. n}

I have an OnCheckChangedListener that listens for flag changes (the flag is set to focus with the false value set, as recommended by http://www.mousetech.com/blog/?p=74 ).

The behavior I'm looking for is that users can check the box to set its state, and they can click on the list item to get a description.

Currently, the state of the checkbox is saved correctly, and if you click on an item, it will show a description. However, if you first change the state and then click to get a description, the checkbox returns to the previous state. Virtually all flags are returned to their previous state.

Does anyone know how I can make this work?

Thanks.

***** EDIT ********

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview_item, cursor, new String[] {MyContentColumns.NAME, MyContentColumns.MyBoolean }, new int[] {R.id.listview_item_title,R.id.listview_item_myBoolean }); adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Cursor cursor,int columnIndex) { String c_name = cursor.getColumnName(columnIndex); if (c_name.equals(MyContentColumns.NAME)) { // set up name and description of listview MyContent v = mycontent.getMyContent(cursor.getString(columnIndex)); if (view instanceof TextView) { TextView tv = (TextView) view; if (tv.getId() == R.id.listview_item_title) { tv.setText(v.getLongName()); } } return true; } else if (c_name.equals(MyContentColumns.MyBoolean)) { // if myBoolean == 0, box is checked // if myBoolean == 1, box is unchecked int myBoolean = cursor.getInt((cursor.getColumnIndex(MyContentColumns.MyBoolean))); final String myCont_name = cursor.getString(cursor.getColumnIndex(MyContentColumns.NAME)); final String myCont_cid = cursor.getString(cursor.getColumnIndex(MyContentColumns.CONTENT_ID)); final long c_id = cursor.getLong(cursor.getColumnIndex(MyContentColumns._ID)); if (view instanceof CheckBox) { ((CheckBox) view).setChecked(myBoolean == 1); ((CheckBox) view) .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { ContentValues values = new ContentValues(); values.put(MyContentColumns.MyBoolean,isChecked ? 1 : 0); int rows = getContentResolver() .update( ContentUris.withAppendedId(Uri.withAppendedPath(MyContentColumns.CONTENT_URI,"mycontent"),c_id), values, null, null); if ( rows == 0) { Logger.wLog(String .format("Couldn't update values for %s into db for content %d", myCont_name, myCont_cid)); } } }); } return true; } return false; } }); 

So it seems that clicking on one list item causes the other list items to get clicked too ... so the values ​​are returned and my states become inconsistent ... ideas? thanks.

+2
android checkbox listview
source share
3 answers

So I managed to fix this by calling cursor.requery () when my checkbox listener was called. Not sure if this is a good method, but it works.

0
source share
  boolean[] itemChecked = new boolean[100]; public View getView(int pos, View inView, ViewGroup parent) { System.out.println(" ImageCursorAdapter : getView : "); View v = inView; // Associate the xml file for each row with the view if (v == null) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = inflater.inflate(R.layout.main, null); } this.c.moveToPosition(pos); final CheckBox checkBox = (CheckBox) v.findViewById(R.id.bcheck); checkBox.setTag(pos); checkBox.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String position = (String) checkBox.getTag(); if (checkBox.isChecked() == true) { itemChecked[Integer.valueOf(position)] = checkBox.isChecked(); } else { itemChecked[Integer.valueOf(position)] = checkBox.isChecked(); } } }); checkBox.setChecked(itemChecked[pos]); return (v); } 
0
source share

decalare the boolean array globally and save the checkbox state in it

 public class CheckBoxAdapter extends SimpleCursorAdapter { private Cursor c; private Context context; private boolean[] itemChecked = new boolean[100]; public CheckBoxAdapter (Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.c = c; this.context = context; } public View getView(int pos, View inView, ViewGroup parent) { View v = inView; final CheckBox checkBox = (CheckBox) v.findViewById(R.id.bcheck); checkBox.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (checkBox.isChecked() == true) { itemChecked[Integer.valueOf(position)] = checkBox.isChecked(); } else{ itemChecked[Integer.valueOf(position)] = checkBox.isChecked(); } } }); checkBox.setChecked(itemChecked[pos]); }//end of getView method }//end of class CheckBoxAdapter 
-one
source share

All Articles