I am filling in my listview with a SimpleCursorAdapter.
By using getView() I set the image to an imageView for each item.
Now I only have 1 image (in the first row) in my cursor. But on scroll it appears in random positions. Why it is so?
code
public class MyListAdapter extends SimpleCursorAdapter{
private Cursor cur;
private Context context;
private SQLiteDatabase db;
public MyListAdapter (Context context, Cursor c, SQLiteDatabase db) {
super(context, R.layout.list_item, c, new String[] {}, new int[] {});
this.cur=super.getCursor();
this.context = context;
this.db=db;
}
private class ViewHolder {
TextView txtTitle, txtDate;
ImageView imgSm;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.txt_title);
holder.txtDate = (TextView) convertView.findViewById(R.id.txt_date);
holder.imgSm= (ImageView) convertView.findViewById(R.id.img_sm);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
this.cur.moveToPosition(position);
holder.txtTitle.setText(this.cur.getString(this.cur.getColumnIndex("title")));
holder.txtDate.setText(this.cur.getString(this.cur.getColumnIndex("date")));
byte[] blob = this.cur.getBlob(this.cur.getColumnIndex("img_sm"));
if (blob!=null) {
holder.imgSm.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0, blob.length));
}
return convertView;
}
}
it becouse ListView recycling …
ListView creating only visible items + few more.
if you scroll invisible items will be reused if they are no longer visible.
you setting only first image(as you said that only first row in cursor has blob data)
so if first row is resused you get a row with image allready setup to fist one.
so use