Search code examples
androidlistviewcursorsimplecursoradapter

Simplecursoradapter with Listview just mixed up while scrolling


Folks.

I've a problem with my simplecursorAdapter. Everything works perfectly except when scrolling the listview it just mixes up the Favorite icon for the rows which included in the custom layout and it just appears randomly while scrolling. Can you guide me what's wrong in my code?

Thanks in advance!

public class AlternateRowCursorAdapter extends SimpleCursorAdapter {

int layoutn;
Cursor localCursor, test;
Bitmap bitImg;
Context localContext;
Bitmap Avatar;
ImageView one, two, three, four, five;
LayoutInflater mInflater;
SQLiteDatabase mDb;
MyDbHelper mHelper;

public static final String TABLE_NAME = "MSGS";
public static final String COL_MsgID = "msgIdc";
public static final String COL_MsgCat = "msgCatC";
public static final String COL_MsgTit = "msgtitleC";
public static final String COL_MsgFavor = "msgFavorC";


public AlternateRowCursorAdapter (Context context, int layout, Cursor c,
        String[] from, int[] to) {

    super(context, R.layout.listtype, c, from, to);

    this.localContext = context;

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {


    View row = super.getView(position, convertView, parent);

    final Cursor cursbbn = getCursor();

    if (row == null)

    {
        row = ((LayoutInflater) localContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.listtype, null);

    }


    final String Title;
    String SandID;
    final String MsgID;
    final String MsgFav;

    Typeface tf = Typeface.createFromAsset(localContext.getAssets(),"fonts/khalaadsara.ttf");

     Title = cursbbn.getString(2);
     SandID = cursbbn.getString(1);
     MsgID=cursbbn.getString(0);
     MsgFav=cursbbn.getString(4);

    TextView titler = (TextView) row.findViewById(R.id.Sandtit);

    titler.setTypeface(tf);
    titler.setText(Title);



    one = (ImageView) row.findViewById(R.id.imageView5);
    two = (ImageView) row.findViewById(R.id.imageView4);
    three = (ImageView) row.findViewById(R.id.ImageView03);
    four = (ImageView) row.findViewById(R.id.ImageView02);
    five = (ImageView) row.findViewById(R.id.imageView1);


    if(MsgFav.contentEquals("YES"))
    {
        one.setImageResource(R.drawable.favorpress);
    }



    return row;

}

}

Edit : Here is my code to refresh the values in Onresume event :

private void refreshvalues() {

    mDb = mHelper.getWritableDatabase();

    curs = mDb.query(MyDbHelper.TABLE_NAME, columns, null, null, null,
            null, null,

            null);

    cursF = mDb.query(TABLE_NAME, columns, COL_MsgFavor + "=" + "?",
            new String[] { "YES" }, null, null, COL_MsgTit + " ASC");

    String[] headers = new String[] {MyDbHelper.COL_MsgTit ,MyDbHelper.COL_MsgID};

    mAdapter = new AlternateRowCursorAdapter(this, R.layout.listtype, curs,
            headers, new int[] { R.id.Sandtit});

    fAdapter = new AlternateRowCursorAdapter(this, R.layout.listtype,
            cursF, headers, new int[] { R.id.Sandtit });

    mList.setAdapter(mAdapter);
    fList.setAdapter(fAdapter);

curs.moveToFirst();
cursF.moveToFirst();

mAdapter.notifyDataSetChanged();
fAdapter.notifyDataSetChanged();

mList.invalidateViews();
fList.invalidateViews();

curs.requery();
cursF.requery();

}

Solution

  • ListView recycles list item Views. So make sure to always call one.setImageResource().

    if (MsgFav.contentEquals("YES")) {
        one.setImageResource(R.drawable.favorpress);
    } else {
        one.setImageResource(R.drawable.default_drawable_as_specified_in_layout);
    }