我有一个ListView看起来像以下内容:
I have a Listview that looks like the following:
复选框:TextView的{0 .. N}
checkbox:textview {0 .. n}
我有,对于改变复选框(复选框具有可调焦设置为false监听所推荐的一个OnCheckChangedListener http://www.mousetech.com/blog/?p=74 )。
I have a OnCheckChangedListener that listens for checkbox changes (checkbox has focusable set to false as recommended by http://www.mousetech.com/blog/?p=74).
这是我找的行为,用户可以点击复选框来设置它的状态,他们可以在ListView项单击获得说明。
The behaviour that I am looking for is that users can click the checkbox to set its state, and they can click on the listview item to get a description.
目前,复选框状态被正确保存,如果你点击一个项目,那么它说明了一个描述。但是,如果你先改变一个状态,然后单击以获取描述,复选框,恢复到以前的状态。事实上所有的复选框恢复到以前的状态。
Currently, checkbox state is saved properly and if you click on an item, then it shows a description. However, if you first change a state and then click to get the description, the checkbox reverts to a prior state. In fact all checkboxes revert back to a prior state.
任何人都知道我可以得到这个工作?
Anyone know how i can get this working?
感谢。
*****EDIT********
*****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 also get clicked... so values revert and my states become inconsistent... ideas? thanks.
所以我设法通过调用cursor.requery()时,我的听众复选框被称为修复它。不知道这是一个很好的方法,但它的工作原理。
So I managed to fix it by calling cursor.requery() when my checkbox listener was called. Not sure if this is a good method, but it works.