Search code examples
androidandroid-asynctaskimageviewuniversal-image-loader

Imageloader inside for loop in android


I need to loop the image load Google map marker with images. So, I am using custom marker( there will more than 50 different marker images). I am using Universal Image loader for loading the image on Image view. Once the image is loaded on the Image view I am converting that view into marker.

My problem is ImageLoadingListener is not getting looped. But if I am placing the breakpoint then It is working fine.

  final View marker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
            final RoundedImageView imv_Logo = (RoundedImageView) marker.findViewById(R.id.imv_size);
            bitmapArray.clear();
            for (int i = 0; i < list_image_url.size(); i++) {
                imageLoader.displayImage(list_image_url.get(i), imv_Logo, options,
                        new ImageLoadingListener() {
                            @Override
                            public void onLoadingStarted(String arg0, View arg1) {

                            }

                            @Override
                            public void onLoadingFailed(String arg0, View arg1,
                                                        FailReason arg2) {
                                imv_Logo.setImageDrawable(getResources().getDrawable(R.drawable.image_not_found));
                                bitmapArray.add(createDrawableFromView(Establishment.this, marker));
                                if (bitmapArray.size() == list_image_url.size()) {
                                    loadmarker(progressDialog);
                                }

                            }

                            @Override
                            public void onLoadingComplete(String arg0, View arg1,
                                                          Bitmap arg2) {
                                bitmapArray.add(createDrawableFromView(Establishment.this, marker));
                                if (bitmapArray.size() == list_image_url.size()) {
                                    loadmarker(progressDialog);
                                }

                            }

                            @Override
                            public void onLoadingCancelled(String arg0, View arg1) {

                            }
                        });
            }

Could any one help me on this?


Solution

  • I can't guarantee it, but I guess the problem is the following. When you tell the ImageLoader to load an image into an ImageView, it will keep an internal HashMap to track which image will be displayed in which ImageView. What you are actually doing here, is asking the ImageLoader to download different images but to display them in the same ImageView.

    imageLoader.displayImage(list_image_url.get(i), imv_Logo,....
    

    So each time you call displayImage, it will actually update the Map and I guess that ImageLoader is smart enough to cancel previous downloads. So in the end, you only download one image.

    While in debug mode, the download will have time to occur between twi calls to displayImage, therefore you will see multiple calls to onLoadingComplete.

    What would make sense is to have several ImageViews.