我正在使用 CursorAdapter 在 ListView 中读取数据库。 我在列表的每个项目中都有一个复选框,当用户选中该复选框时,我数据库中最喜欢的列更改为是,并将该项目添加到收藏夹。
一切正常,最喜欢的列已更改,但是当我上下滚动列表时,复选框将取消选中。 如果您重新启动应用程序,则复选框已被选中
这个问题我该怎么办:
抱歉我的英语不好:
CursorAdapter 类:
public class MyAdapter extends CursorAdapter {
Context b;
LayoutInflater inflater;
@SuppressWarnings("deprecation")
public MyAdapter(Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
b= (Context) context;
}
@SuppressWarnings("unused")
@Override
public void bindView(View view, Context context, final Cursor cursor) {
// TODO Auto-generated method stub
TextView tv1 = (TextView)view.findViewById(R.id.txt_name);
TextView tv2 = (TextView)view.findViewById(R.id.txt_numer);
tv1.setText(cursor.getString(2));
tv2.setText(cursor.getString(3));
final int pos = cursor.getPosition();
final CheckBox repeatChkBx = (CheckBox)view.findViewById(R.id.favorite_check);
String me = cursor.getString(cursor.getColumnIndex("like"));
if (me.equals("yes")) {
repeatChkBx.setChecked(true);
} else {
repeatChkBx.setChecked(false);
}
repeatChkBx.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
MyDatabase MyDatabase = new MyDatabase(b);
SQLiteDatabase mydb = MyDatabase.getWritableDatabase();
cursor.moveToPosition(pos);
if (repeatChkBx.isChecked()) {
mydb.execSQL("update list set like = 'yes' where id = " + cursor.getString(1));
}else{
mydb.execSQL("update list set like = 'no' where id = " + cursor.getString(1));
}
}
});
}
protected Context getActivity() {
// TODO Auto-generated method stub
return null;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.item, parent, false);
}
}
截图:
最佳答案
勾选的元素不回收。 您必须将选中的项目保存到某种数组中 - 动态或静态。 bool 数组非常适合此目的。
Boolean[] myCheckedItems = new Boolean[SIZE];
关于android - ListView 中的 CursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23716432/