views:

82

answers:

1

Hi,

I believe this is pretty trivial but I can't get it to work.
I want to display a default image in gallery elements (ImageViews) while their actual image is being fetched from the net.
Right now, nothing is shown for an ImageView which its image has yet to arrive. Once it arrives it is immediately shown.
What I tried is right after the instantiation of the ImageView to call its setImageResource function like so:

final ImageView i = new ImageView(mContext);
i.setImageResource(R.drawable.loading);

But it doesn't seem to work. Below is the full getView() function.
Any help is appreciated.
Thanks.

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

     final ImageView i = new ImageView(mContext);
     i.setImageResource(R.drawable.loading);


     // if the drawbale is in the buffer - fetch it from there
     Drawable bufferedImage = DataManager.getInstance().getImagesBuffer()[position];
     if (bufferedImage != null){
         i.setImageDrawable(bufferedImage);

         BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
         drawable.setAntiAlias(true);
     }
     // if drawable is not in buffer - fetch it from the net via AsyncImageLoader
     else
     {

         String imageUrl = DataManager.getInstance().getImageBufferInstance().getImageUrl(position);
         Drawable downloadedImage = AsyncImageLoader.getInstance().loadDrawable(imageUrl, new ImageCallback() {
         public void imageLoaded(Drawable imageDrawable, String imageUrl) {

                if (imageDrawable == null)
                {
                    imageDrawable = getResources().getDrawable(R.drawable.icon);
                }
                i.setImageDrawable(imageDrawable);

                BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
                drawable.setAntiAlias(true);    

                }
            });

         i.setImageDrawable(downloadedImage);
     }

     i.setLayoutParams(new CoverFlow.LayoutParams(Utils.getInstance().getScreenWidth() / 2,
             Utils.getInstance().getScreenHeight() / 2));
     i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 


     return i;


 }
A: 

Fix your indentation... If I'm reading that correctly, you're only setting the temporary drawable icon in the imageLoaded callback of your (not shown) AsyncImageLoader, which I assume means it's then only being set after the image downloads and is then immediately overwritten with the downloaded image. Try moving the placeholder-setting code into your else block outside the callback.

Yoni Samlan
Yoni, thank you very much for looking into it (and sorry if my indentation made the code unclear...). Your insight helped me understand that the variable downloadedImage is null and I am setting the imageView i with null in i.setImageDrawable(downloadedImage). That is why I don't see anything in the imageView. Once the callback kicks in, it sets i with the actual image and it appears on screen. Instead of doing i.setImageDrawable(downloadedImage) I should ask if downloadedImage is null. If so, set i with a default image. If not, set it with downloadedImage. Again, Thanks for your help.
Rob