Search code examples
androidandroid-volleynetworkimageview

display NetworkImageView's default image without network request


I have a ListView displaying things, many of which have an illustrative image and some of which don't. The ViewHolder uses NetworkImageView to display the illustrations.

The problem is that even if a default image is set, it won't be displayed until one calls setImageUrl(), which in turn will set the source bitmap of the view to null if the url is empty. If the url isn't empty, it makes a request. This means that one is forced to make a network request even if there isn't a valid network url associated with that particular view, otherwise instead of displaying the default image it displays an empty view.

(issue filed: https://code.google.com/p/android/issues/detail?id=59538)

Is there a way around making bogus network requests for items without a valid url?


Solution

  • This works just fine for me:

        String url = getURL();
        NetworkImageView niv = (NetworkImageView)rootView.findViewById(R.id.niv);
        if(url.length() > 0)
            niv.setImageUrl(url, imageLoader);
        niv.setDefaultImageResId(R.drawable._default);
        niv.setErrorImageResId(R.drawable.error);