Can you guys explain why cursor reference being passed for each row as different instance finally comes to mDBHelper.updateFavorites() pointing to its latest position. Because I’m expecting the cursor passed to onClick() method is where actually Cursor c is pointing at the moment of this object passing, but this is not the case. Thank you very much.
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
View row;
final Cursor cursor = c; // This is different for each row in list view
LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
this.defaultDrawable = (Drawable) row.getBackground();
final CheckBox mStar = (CheckBox) row.findViewById(R.id.star);
mStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mStar.isChecked()) {
mDbHelper.updateFavorite(cursor, 1); // Here I'm expecting to have the cursor pointing exactly where Cursor c (which is passed to newView() method) is pointing right now.
} else {
mDbHelper.updateFavorite(cursor, 0); // The same here
}
}
});
ViewHolder holder = new ViewHolder(row);
row.setTag(holder);
return (row);
}
As I can see, you are not making any copies of the cursor. So actually, that is just a reference to the only one cursor.
So if your are assigning it in some loop, moving each time the initial reference to the net position – all your cursors will be moved also.
Is that how it happen in your case?
Good luck