0

I would like to have a little explanation about this behaviour :

I have created a listview. Each item of the listview has an imageview which is fill with image from URL.

When i scroll down to the list, each image is loaded when the item appears => OK

But, when i scroll up, images that have been loaded previously are again downloaded.

How can i stop this behaviour ?

Here is the code of my downloader :

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

And here is a piece of code of my adapter :

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

        Activity activity = (Activity) getContext();

        LayoutInflater inflater = activity.getLayoutInflater();

        View rowView;
        ArticleHome article = getItem(position);


        if (position == 0) {
            rowView = inflater.inflate(R.layout.item_ligne_home_premier, null);
            new DownloadImageTask((ImageView) rowView.findViewById(R.id.imgimg)).execute(article.getImage());
            TextView textView = (TextView) rowView.findViewById(R.id.titlenew);
            textView.setText(article.getTitle());
            textView.setTypeface(faceLight);
        }
        else {
            rowView = inflater.inflate(R.layout.item_ligne_home, null);
            new DownloadImageTask((ImageView) rowView.findViewById(R.id.imgimg)).execute(article.getImage());
            TextView title = (TextView) rowView.findViewById(R.id.titlearticleothers);
            title.setText(article.getTitle());
            title.setTypeface(faceBold);
            TextView desc = (TextView) rowView.findViewById(R.id.descriptionarticleothers);
            // Add test si < a une certaine longeuryr
            desc.setText(article.getDescription().substring(0, 100));
            desc.setTypeface(faceLight);
        }


//      img.setImageAlpha(1680);
//      img.setImageAlpha(1880); // Pasmal plus haut plus foncé


        return rowView;

    } 
wawanopoulos
  • 9,614
  • 31
  • 111
  • 166

1 Answers1

0

The way to actually go about it is to cache each image you download and update the ListView item with the image from Cache , and only download if the image is not present in cache.

This will avoid the download.

Check this answer by Pramod J George , in particular the ImageLoader class .

Community
  • 1
  • 1
SysHex
  • 1,750
  • 2
  • 17
  • 27