Here is how I have implemented bindView:
@Override
public void bindView(View view, Context context, Cursor cursor)
{
final ViewHolder holder = (ViewHolder) view.getTag();
String name = (cursor.getString(cursor.getColumnIndexOrThrow("NotificationDateFor")));
String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
System.out.println("cursor.getPosition(test): "+cursor.getPosition());
holder.nametext.setText(name);
// setImage(image, holder.iv);
holder.chk.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
if (holder.chk.isChecked())
{
itemChecked.set(cursor.getPosition(), true);
System.out.println("cursor.getPosition(true): "+cursor.getPosition());
}
else if (!holder.chk.isChecked())
{
itemChecked.set(cursor.getPosition(), false);
System.out.println("cursor.getPosition(false): "+cursor.getPosition());
// AddDelete
}
}
});
holder.chk.setChecked(itemChecked.get(cursor.getPosition()));
}
The problem occurs at itemChecked.set(cursor.getPosition(), true);
The description of the problem suggests me:
Cannot refer to a non-final variable cursor inside an inner class defined in a different method, change the modifier of Cursor to final.
However if I do final Cursor cursor
, the position value inside the onClick does not get refreshed on scroll. I always get value of 5 if I do a System.out.println("cursor.getPosition(true): "+cursor.getPosition());
and my checkbox check gets messed up again.