【问题标题】:Objects in HashMap - pass to ListView adapterHashMap 中的对象 - 传递给 ListView 适配器
【发布时间】:2012-01-25 21:36:13
【问题描述】:

是否可以在同一个 HashMap 中将字符串和可绘制对象作为对象传递,然后使用此 hashmap 通过 SimpleAdapter 填充 ListView ?

我想要这个,因为我首先获得了 JSON 数据,其中还包含缩略图的 URL。然后我下载这个缩略图。相关代码(我认为):

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,R.id.thumb = ImageView

这适用于标题字符串,但不适用于可绘制对象。这种做法是不是很愚蠢?

提前致谢。

【问题讨论】:

    标签: android listview hashmap listadapter


    【解决方案1】:

    您应该为列表中的行表示创建一个 XML 布局文件,并为您的行增加一个自定义适配器

    【讨论】:

    • 这是一个SimpleAdapter,我宁愿尝试提供一个ViewBinder,然后再去实现我自己的自定义适配器。
    【解决方案2】:

    据我从the SimpleAdapter documentation 得知,ImageViews 仅支持与资源标识符或String(我的意思是must represent an image resource or an image URI)绑定。直接传Drawable对象是不行的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      相关资源
      最近更新 更多