gpt4 book ai didi

android - 带复选框的游标 ListView

转载 作者:行者123 更新时间:2023-11-29 14:16:24 25 4
gpt4 key购买 nike

我有一个如下所示的 ListView :

复选框: TextView {0 .. n}

我有一个 OnCheckChangedListener 用于监听复选框更改(按照 http://www.mousetech.com/blog/?p=74 的建议,复选框的可聚焦设置为 false)。

我正在寻找的行为是用户可以单击复选框来设置其状态,他们可以单击 ListView 项来获取描述。

目前,复选框状态已正确保存,如果您单击某个项目,则会显示描述。但是,如果您先更改状态然后单击以获取描述,则复选框会恢复到之前的状态。事实上,所有复选框都会恢复到之前的状态。

有人知道我怎样才能让它工作吗?

谢谢。

*****编辑 ********

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

所以似乎单击一个列表项会导致其他列表项也被单击...因此值恢复并且我的状态变得不一致...想法?谢谢。

最佳答案

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

}

关于android - 带复选框的游标 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4586442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com