4

Is it possible to pass both strings and drawables as objects in the same HashMap, and then use this hashmap to populate a ListView via SimpleAdapter ?

I want this because I first get JSON data which also contains the URL to a thumbnail. Then I download this thumbnail. Relevant code (I think):

for (...) {
    ...
    InputStream is = (InputStream)content;

    Drawable image = Drawable.createFromStream(is, "src");

    // Hashmap          
    HashMap<String, Object> map = new HashMap<String, Object>();

    map.put("title", new String(jsonObject.getString("Title")));
    map.put("thumb", image);

    mylist.add(map);
}

ListAdapter adapter = new SimpleAdapter(getActivity(), mylist, R.layout.listitem,
        new String[] { "title", "thumb"},
        new int[] { R.id.title, R.id.thumb });

setListAdapter(adapter);

R.id.title = TextView, and R.id.thumb = ImageView

This works for the title string, but not for the drawable. Is this approach just stupid?

Thanks in advance.

Kjell-Bear
  • 759
  • 1
  • 5
  • 12

2 Answers2

1

you should create a XML layout file for your row representation in the list and a custom adapter that inflate your row

Gal Ben-Haim
  • 17,433
  • 22
  • 78
  • 131
0

As far as I can tell from the SimpleAdapter documentation, ImageViews are supported only for binding with a resource identifier or a String, which (the String, I mean) must represent an image resource or an image URI. It won't work if you pass it directly a Drawable object.

Giulio Piancastelli
  • 15,368
  • 5
  • 42
  • 62