I have a custom SimpleCursorAdapter that receives information from a database. If one value is 1, I am the background color of the ImageView, if it is 0, I do not color it. When the ListView is loaded, everything is correct, but then if I scroll through the items in the list, you will get the wrong image.
============= Just loaded ============= After scrolling up and down ====


I know about the processing of ListView and that when one item goes off the screen, its resources are freed up, but I donβt understand why, when they return to the screen, they are loaded incorrectly. I got it by simply setting the ImageView color to transparent when I don't want it to be colored: is this a workaround - the only way to make it work correctly?
This is my corresponding code:
RecipeCursorAdapter.java
public class RecipeCursorAdaptor extends SimpleCursorAdapter { private final Context mContext; private final int mLayout; private final Cursor mCursor; private final LayoutInflater mLayoutInflater; public CustomCursorAdaptor(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.mLayout = layout; this.mContext = context; this.mCursor = c; this.mLayoutInflater = LayoutInflater.from(mContext); } private final class ViewHolder { TextView txt_title; TextView txt_time; TextView txt_difficulty; ImageView img_color; } public View getView(int position, View convertView, ViewGroup parent) { if (mCursor.moveToPosition(position)) { ViewHolder viewHolder; if (convertView == null) { convertView = mLayoutInflater.inflate(mLayout, null); viewHolder = new ViewHolder(); viewHolder.img_color = (ImageView) convertView.findViewById(R.id.ricettaColore); viewHolder.txt_title = (TextView) convertView.findViewById(R.id.ricettaTitolo); viewHolder.txt_time = (TextView) convertView.findViewById(R.id.ricettaTempo); viewHolder.txt_difficulty = (TextView) convertView.findViewById(R.id.ricettaDifficolta); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } String title = mCursor.getString(1); String time = mCursor.getString(2); String difficulty = mCursor.getString(3); int vegetarian = mCursor.getInt(4); viewHolder.txt_title.setText(title); viewHolder.txt_time.setText(time); viewHolder.txt_difficulty.setText(difficulty); if(vegetarian == 1) viewHolder.img_color.setBackgroundColor(0xff669900); } return convertView; }
list_ricette.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView android:id="@+id/ricettaColore" android:layout_width="5dip" android:layout_height="45dip" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:contentDescription="Ricetta vegetariana" /> <TextView android:id="@+id/ricettaTitolo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_toRightOf="@+id/ricettaColore" android:scrollHorizontally="false" android:singleLine="true" android:text="Titolo Ricetta" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/ricettaDifficolta" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/ricettaTitolo" android:layout_below="@+id/ricettaTitolo" android:scrollHorizontally="false" android:singleLine="true" android:text="DifficoltΓ " android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#A1A1A1" /> <TextView android:id="@+id/ricettaTempo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBottom="@+id/ricettaDifficolta" android:layout_alignParentRight="true" android:layout_below="@+id/ricettaTitolo" android:gravity="right" android:text="Tempo" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#A1A1A1" />
source share