问题
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?
回答1:
To avoid triggering callbacks on listener, you should unregister existing one by cb.setOnCheckedChangeListener(null)
then set cb.setChecked(c.getBoolean(c.getColumnIndex("state")))
then cb.setOnCheckedChangeListener()
again.
However you should think of a better way of using that OnCheckedChangeListener
, since you would end up creating listeners more than needed.
回答2:
private SparseBooleanArray bolarray;
public class ViewHolder{
CheckBox checked_displayName;
int contact_id;
String displayName;
boolean isChecked=false;
}
public void bindView(View v, Context context, Cursor cursor)
{
ViewHolder viewHolder=new ViewHolder();
viewHolder.checked_displayName=(CheckBox)
v.findViewById(R.id.checked_displayName);
viewHolder.displayName=cursor.getString(cursor.getColumnIndex(PROJECTION[0]));
viewHolder.checked_displayName.setText(viewHolder.displayName);
viewHolder.checked_displayName.setTag(viewHolder);
if(!bolarray.get(viewHolder.contact_id)){
bolarray.put(viewHolder.contact_id, false);
viewHolder.checked_displayName.setChecked(false);
viewHolder.isChecked=false;
}else{
bolarray.put(viewHolder.contact_id, true);
viewHolder.checked_displayName.setChecked(true);
viewHolder.isChecked=true;
}
来源:https://stackoverflow.com/questions/13566648/checkbox-listener-in-listview-with-cursoradapter