I have a listview which displays food deliveries and its items are set by a custom SimpleCursorAdapter.
I am displaying an extra icon for each food delivery or listview item that includes special offers etc. I am also sorting the list so the food deliveries with the special offers to be on top.
When the listview is first initialized, everything is good. When you scroll then items that do not include special offer have also the icon and the listview is messed up.
I guess the problem is coming from the view recycling but I do not know how to solve it.
Here is my SimpleCursorAdapter class
private class MyCursorAdapter extends SimpleCursorAdapter{
private int rowLayout;
private Cursor cursor;
public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.rowLayout=layout;
this.cursor=c;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(this.rowLayout,null);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// get resources from database
int id = cursor.getInt(0);
int title = cursor.getInt(1);
final int specialOffer = cursor.getInt(7);
if (specialOffer==1){
// we have a special offer delivery
view.findViewById(R.id.offer_imageview).setVisibility(View.VISIBLE);
}
}
}
if (specialOffer==1){
// we have a special offer delivery
view.findViewById(R.id.offer_imageview).setVisibility(View.VISIBLE);
}
else{
view.findViewById(R.id.offer_imageview).setVisibility(View.GONE);
}