【发布时间】:2014-05-17 21:53:49
【问题描述】:
我正在使用 CursorAdapter 在列表视图中读取数据库。 我在列表的每个项目中都有一个复选框,当用户选中该复选框时,我的数据库中的收藏列将更改为“是”并将该项目添加到收藏夹中。
一切正常,最喜欢的列发生了变化,但是当我向上和向下滚动列表时,复选框将被取消选中。 如果您重新启动应用程序,复选框已被选中
这个问题我该怎么办:
抱歉我的英语不好:
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);
}
}
截图:
【问题讨论】:
-
有人回答这个问题吗
-
问题是 ListView 不知道确切点击了哪个项目,您可以通过 setTag 和 getTag 为每个项目做到这一点
标签: android listview checkbox android-cursoradapter