1

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

mfrachet
  • 8,772
  • 17
  • 55
  • 110

2 Answers2

2

Get the latest version of Ion and use the following.

Ion.with(holder.img)
.load(rowItem.getImage());

This method will load asynchronously.

Your current usage is blocking the UI thread. Ion should properly handle convertView recycling, so that is not an issue.

koush
  • 2,972
  • 28
  • 31
-1

This is lazy loading problem, go with Universal image loader: https://github.com/nostra13/Android-Universal-Image-Loader https://github.com/nostra13/Android-Universal-Image-Loader/blob/master/sample/src/com/nostra13/example/universalimageloader/ImagePagerActivity.java

VINIL SATRASALA
  • 614
  • 5
  • 11