I m facing a problem when I want to load images from an adapter using the ion library.
In fact, I have items with a string corresponding to the url of the iconic image that I want to load for each item on my gridview.
The problem is due to the adapter view management (reusing existing view if I m not wrong), and I dont know how to bypass this...
For example, if I load 10 elements with an image, the first time it works. Then, when I scroll to bottom, and then I scroll to top, the image changes (due to the reuse of the existing view...)
Can you help me ?
This is my adapter code :
public class ProtocoleAdapter extends BaseAdapter {
private Context context;
private List<ProtocoleItem> mListe;
public ProtocoleAdapter(Context context, List<ProtocoleItem> liste) {
this.context = context;
this.mListe = liste;
}
private class ViewHolder {
TextView txtTitre;
ImageView img;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater
.inflate(R.layout.grid_item, null);
holder = new ViewHolder();
holder.txtTitre = (TextView) convertView
.findViewById(R.id.grid_item_label);
holder.img = (ImageView) convertView
.findViewById(R.id.grid_item_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ProtocoleItem rowItem = mListe.get(position);
boolean isLoaded = false;
try {
Bitmap bitmap = Ion.with(context)
.load(rowItem.getImage())
.asBitmap()
.get();
isLoaded = true;
holder.img.setImageBitmap(bitmap);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
if (!isLoaded) {
if (position % 5 == 0) {
holder.img.setBackgroundColor(0xff176799);
} else {
if (position % 4 == 0) {
holder.img.setBackgroundColor(0xff2F87B0);
} else {
if (position % 3 == 0) {
holder.img.setBackgroundColor(0xff42A4BB);
} else {
if (position % 2 == 0) {
holder.img.setBackgroundColor(0xff5BC0C4);
} else {
holder.img.setBackgroundColor(0xff78D6C7);
}
}
}
}
}
holder.txtTitre.setText(rowItem.getTitre());
return convertView;
}
Thanks for all !
Have a good day