我有一个类似于以下内容的Listview:
复选框:textview {0 .. n}
我有一个OnCheckChangedListener,用于侦听复选框更改(复选框已按照http://www.mousetech.com/blog/?p=74的建议将焦点设置为false)。
我正在寻找的行为是用户可以单击复选框来设置其状态,他们可以单击列表视图项来获取描述。
目前,正确保存了复选框状态,如果单击某个项目,则会显示说明。但是,如果首先更改状态然后单击以获取描述,则复选框将恢复为先前状态。事实上,所有复选框都会恢复到先前的状态。
任何人都知道我怎么能让这个工作?
感谢。
的 的 ** * ** 修改的 * ** * ****
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;
}
});
所以似乎点击一个列表项会导致其他列表项也被点击...所以值恢复,我的状态变得不一致......想法?感谢。
答案 0 :(得分:0)
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);
}
答案 1 :(得分:0)
所以当我的复选框监听器被调用时,我设法通过调用cursor.requery()来修复它。不确定这是否是一个好方法,但它确实有效。
答案 2 :(得分:-1)
全局声明一个布尔数组并将复选框状态保存在其中
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