Android HashMap中的对象-传递到ListView适配器
是否可以在同一个HashMap中同时传递字符串和Drawable作为对象,然后使用此HashMap通过SimpleAdapter填充ListView 我之所以要这样做,是因为我首先得到JSON数据,其中还包含指向缩略图的URL。然后我下载这个缩略图。相关代码(我认为): (…)的Android HashMap中的对象-传递到ListView适配器,android,listview,hashmap,listadapter,Android,Listview,Hashmap,Listadapter,是否可以在同一个HashMap中同时传递字符串和Drawable作为对象,然后使用此HashMap通过SimpleAdapter填充ListView 我之所以要这样做,是因为我首先得到JSON数据,其中还包含指向缩略图的URL。然后我下载这个缩略图。相关代码(我认为): (…)的{ ... InputStream是=(InputStream)内容; Drawable image=Drawable.createFromStream(是“src”); //哈希映射 HashMap=newHashMa
{
...
InputStream是=(InputStream)内容;
Drawable image=Drawable.createFromStream(是“src”);
//哈希映射
HashMap=newHashMap();
map.put(“title”),新字符串(jsonObject.getString(“title”);
地图放置(“拇指”,图像);
添加(地图);
}
ListAdapter=new SimpleAdapter(getActivity(),mylist,R.layout.listitem,
新字符串[]{“title”,“thumb”},
新int[]{R.id.title,R.id.thumb});
setListAdapter(适配器);
R.id.title=TextView,R.id.thumb=ImageView
这适用于标题字符串,但不适用于可绘制字符串。这种方法是愚蠢的吗
提前感谢。您应该为列表中的行表示创建一个XML布局文件,并创建一个自定义适配器,用于对行进行充气。据我所知,
ImageView
s仅支持与资源标识符或字符串绑定,而字符串就是字符串。如果您直接向它传递一个可绘制的
对象,它将不起作用。这是一个SimpleAdapter
,我宁愿在实现我自己的自定义适配器之前尝试提供一个ViewBinder
。
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);