Ostkontentitan
Ostkontentitan

Reputation: 7010

Volley NetworkImage in ArrayAdapter -> load Drawable-Res conditionally

I am using volleys NetworkImageView to load image´s in an ArrayAdapter.

For some Items in my Adapter i want to use a Drawable resource instead.

Unfortunally setting the ImageResource doesnt seem to have an effect on NetworkImage.

    networkImage.setImageResource(R.drawable.mydrawable); // no effect

I tryed working without NetworkImageView using an ImageLoaders get() method providing an interface. Unfortunally the view recycling didnt work out anymore and image downloads overrode already recycled and reused views.

    ImageLoader.ImageListener listener = 
    ImageLoader.getImageListener(image, R.drawable.loading, R.drawable.error);
    ImageCacheManager.getImageLoader().get(url,listener);
    // Override´s recycled views image

Any solutions?

Upvotes: 3

Views: 1828

Answers (1)

Oleksandr Yefremov
Oleksandr Yefremov

Reputation: 1404

If you only want to show drawable and not load any image at all for specific item, this is quite easy:

public void bind(Item item) {
  if (item.shouldDisplayDrawable()) {
    imageView.setDefaultImageResId(R.drawable.my_drawable); // show your drawable
    imageView.setImageUrl(null, app.getImageLoader()); // and if url == null, Volley will not load any image to replace Default 
  } else {
    imageView.setImageUrl(item.getImageUrl(), app.getImageLoader()); // load image from url
  } 
}

I tried this in my Adapter, recycling works without any issues. And you can continue using

imageView.setDefaultImageResId(R.drawable.icon_image_default);
imageView.setErrorImageResId(R.drawable.icon_image_error);

on imageView you download from url, it will display correct drawable while loading and on error.

Upvotes: 7

Related Questions