0

I want to set an image to a list view using a hash map, currently all data except the image is being set. How do i set the image using the hash map? The images are stored in the drawable folder. Below is my code.

    // create a List of Map<String, ?> objects
    ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

    for (Attraction attraction : attractions) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(attraction_name, attraction.getName());
        map.put("img", String.valueOf(R.drawable.king));
        data.add(map);
    }

    // create the resource, from, and to variables
    int resource = R.layout.layout_att;

    String[] from = {attraction_name, "img"};
    int[] to = {R.id.name, R.id.img};

    // create and set the adapter
    SimpleAdapter adapter =
            new SimpleAdapter(this, data, resource, from, to);

    attractionListListView.setAdapter(adapter);

    attractionListListView.setOnItemClickListener(this);

}
mmrrtt
  • 11
  • 1
  • 4

2 Answers2

0

You can use: http://developer.android.com/reference/android/widget/ImageView.html#setImageResource(int)

in your adapter, do something like:

imageView.setImageResource((int) hashMap.get("img"));

although, I'd really recommend you to have a class instead of a hash map to do such a thing. This way you would have a List of your class (it could be even a inner class just to help you organize) instead of a hash map. With this you would avoid casting the hashmap content to int.

But other then that if you really want to use the hashmap, you can use the code I've written above.

Regards

luis.mazoni
  • 1,084
  • 8
  • 13
0

I don't think that you need to keep binary data (stream) in Map, it can be good if you read it when needed. You can not handle binary image as String..

See this thread for a solution: How to display a list of images in a ListView in Android?

Community
  • 1
  • 1
Laszlo Lugosi
  • 3,669
  • 1
  • 21
  • 17