0

I'm attempting parse a JSON payload and place the information in Relative Layout View.

I use this STANDARD asynchronous download routine to obtain the bitmaps to be placed in the appropriate ImageView:

public class AsyncDownloadImage extends AsyncTask<String,Void,Bitmap>{

        ImageView bmImage;

        public AsyncDownloadImage(ImageView bmImage){

                this.bmImage = bmImage;
        }


        @Override
        protected Bitmap doInBackground(String... urls) {

            String urldisplay = null;
            urldisplay = urls[0];

            Bitmap merchViewBitMap = Bitmap.createBitmap(90,90,Bitmap.Config.ARGB_8888);


            try{

                InputStream in = new URL(urldisplay).openStream();
                merchViewBitMap = BitmapFactory.decodeStream(in);

                in.close();

            }catch(Exception e){
                System.out.println(String.format("Image Download Exception ==> %s", e.toString()));

            }

            return merchViewBitMap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {

                super.onPostExecute(result);
                bmImage.setImageBitmap(result);

        }
    }

It's called this way in the getView method of the Adapter class:

String imageUrl = String.format("https://www.xxxxxx.com/va/public/render_image?image_id=%s", currentMerchItem.getMt_image_id());

new AsyncDownloadImage(viewHolder.imageview).execute(imageUrl);

The images are downloaded and the ImageView(s) are set, but they are downloaded repeatedly and infinitely. Multiple images seem to be in an ImageView and when scrolled through they change as each is gif.

How do I stop this after the last image in the list is downloaded?!

-TU

T. Ujasiri
  • 317
  • 1
  • 3
  • 14
  • It looks like you failed to include the code containing the problem. I would say the problem is that your `new AsyncDownloadImage(viewHolder.imageview).execute(imageUrl);` keeps getting called. – wvdz Oct 31 '15 at 20:30

1 Answers1

1

The adapters getView method will be called each time the view of the adapter (listview for example) needs to refresh item's view. When you scroll for example you will get many calls to the getview method (depends on the list length ofcourse). At least you need to manage which images are already being loaded from the net in your case. You also have a lot of open source code for lazy loading exactly for cases like yours.

You can also check this thread, it may help you: Lazy load of images in ListView

Community
  • 1
  • 1
MikeL
  • 5,385
  • 42
  • 41