android – image loading not working while passing HashMap to custom array adapter of Universal Image Loader-ThrowExceptions

Exception or error:

I need to show the images which I have in my drawable folder. I created a integer array to store the drawable ids like so

private int[] itemImages = new int[]{
            R.drawable.image1, R.drawable.image2, R.drawable.image3,
            R.drawable.image4, R.drawable.image5, R.drawable.image6,
            R.drawable.image7, R.drawable.image8, R.drawable.image9,
            R.drawable.image10, R.drawable.image11, R.drawable.image12,
            R.drawable.image13, R.drawable.image14, R.drawable.image15,
            R.drawable.image16, R.drawable.image7, R.drawable.image18,
            R.drawable.image19, R.drawable.image20, R.drawable.image21,
            R.drawable.image22, R.drawable.image23, R.drawable.image24,
            R.drawable.image25, R.drawable.image26, R.drawable.image27
    };

and some other texts itemPrice and itemNames
then created a List and passed the elements to the custom array adapter

  List<HashMap<String, String>> imageArray = new ArrayList<HashMap<String, String>>();

           for (int i = 0; i < itemImages.length; i++) {
                HashMap<String, String> hm = new HashMap<String, String>();
                hm.put("name", "Name : " + itemNames[i]);
                hm.put("price", "Price : " + itemPrices[i]);
                hm.put("image", Integer.toString(itemImages[i]));
                imageArray.add(hm);
            }
    Custom_Adapter adapter = new Custom_Adapter(getApplicationContext(),
R.layout.imagelist_layout, imageArray);

the getView in the custom array adapter is

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View displeyView = inflater.inflate(R.layout.imagelist_layout, parent, false);
    ImageView img = (ImageView) displeyView.findViewById(R.id.image);
    TextView name = (TextView) displeyView.findViewById(R.id.name);
    TextView price = (TextView) displeyView.findViewById(R.id.price);

    try {
        final HashMap<String, String> imgList = imageList.get(position);
        final String image = imgList.get("image");
        final String price1 = imgList.get("price");
        final String name1 = imgList.get("name");

        imageLoader.displayImage(image, img, options);
        name.setText(name1);
        price.setText(price1);
    } catch (Exception e) {
    }
    return displeyView;
}

by doing this the texts are showing. but I’m unable to view the images. the R.drawable.errorimage is displayed.
How can I solve this?

How to solve:

If you are just trying to set a drawable image to your imageview’s background you can set it just with

imageView.setImageResource(yourDrawable);

But i think you have some memory problems or need to use Display Options of Unviersal Image Loader such download as a rounded bitmap etc.

You can set drawable with universal image loader:

String yourUrl = "drawable://" + R.drawable.your_drawable;
            com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(yourUrl, yourImageView, displayOptions);

I hope this’ll help you.

###

Universal image loader helps you to load images in internet with good effect and for loading local images prefering native way to load drawables — ImageView.setImageResource(…) instead of using of ImageLoader is good according to me…

use

img.setImageResource(image);

instead of

imageLoader.displayImage(image, img, options);

for uil

first of all change your array

private String[] itemImages = new String[]{

    "drawable://R.drawable.image1","drawable://R.drawable.image2", 
    ...
};



   List<HashMap<String, String>> imageArray = new ArrayList<HashMap<String, String>>();

           for (int i = 0; i < itemImages.length; i++) {
                HashMap<String, String> hm = new HashMap<String, String>();
                hm.put("name", "Name : " + itemNames[i]);
                hm.put("price", "Price : " + itemPrices[i]);
                hm.put("image", (itemImages[i]));
                imageArray.add(hm);
            }
    Custom_Adapter adapter = new Custom_Adapter(getApplicationContext(),

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View displeyView = inflater.inflate(R.layout.imagelist_layout, parent, false);
    ImageView img = (ImageView) displeyView.findViewById(R.id.image);
    TextView name = (TextView) displeyView.findViewById(R.id.name);
    TextView price = (TextView) displeyView.findViewById(R.id.price);

    try {
        final HashMap<String, String> imgList = imageList.get(position);
        final String image = imgList.get("image");
        final String price1 = imgList.get("price");
        final String name1 = imgList.get("name");

        imageLoader.displayImage(image, img, options);
        name.setText(name1);
        price.setText(price1);
    } catch (Exception e) {
    }
    return displeyView;
}

###

Try This Code:-

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View displeyView = inflater.inflate(R.layout.imagelist_layout, parent, false);
    ImageView img = (ImageView) displeyView.findViewById(R.id.image);
    TextView name = (TextView) displeyView.findViewById(R.id.name);
    TextView price = (TextView) displeyView.findViewById(R.id.price);

    try {
        final HashMap<String, String> imgList = imageList.get(position);
        final String image = imgList.get("image");
        final String price1 = imgList.get("price");
        final String name1 = imgList.get("name");


Drawable drawable = getResources().getDrawable(getResources()
                    .getIdentifier(Integer.parseInt(image), "drawable", getPackageName()));

            img.setImageDrawable(drawable);  


        name.setText(name1);
        price.setText(price1);
    } catch (Exception e) {
    }
    return displeyView;
}

Leave a Reply

Your email address will not be published. Required fields are marked *