I have a problem very similar than this post. In every row of my ListView I have a checkbox with a listener. The Listener update databse row.
@Override
public void bindView(View v, Context context, Cursor c) {
TextView tvA = (TextView) v.findViewById(R.id.adi_tv_activity);
CheckBox cb = (CheckBox) v.findViewById(R.id.adi_cbox);
tvA.setText(c.getString(c.getColumnIndex("name")));
final long id = c.getLong(c.getColumnIndex("_id"));
final Context ctx = context;
cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ObjDBF dbf = new ObjDBF(ctx); //Object thet update db
if(isChecked) {
dbf.tbActivitiesUpdateState(id, true);
} else {
dbf.tbActivitiesUpdateState(id, false);
}
}
});
if (c.getBoolean(c.getColumnIndex("state"))) {
cb.setChecked(true);
} else {
cb.setChecked(false);
}
}
I have 2 problems.
- Scrolling problem. Moving the list I lost the checkbox state for row that disappear from screen.
- Any time I use setChecked the listenr is called causing a new db update.
For risolving scrolling problem I wanted to use adapter.changeCursor every time I update db, but for second problem it cause a loop.
I try also to use a use an Array as in the second answer of above linked post, but it use the method getView od adapter, I have a cursorAdapter and do work in newView and bindView
How can I solve the problem?
edit: auselen solution works but it creates and destroys a lot of listeners, there is an other more efficient solution?