[英]Android ListView with images from special hashmap
I try to handle a hashmap to show a listview with images. 我尝试处理一个哈希图以显示带有图像的列表视图。 I search for an answer like here: How to display Images in ListView android . 我在这里搜索答案: 如何在ListView android中显示图像 。 But it doesn't work... 但这行不通...
My Code is: 我的代码是:
for (int i = 0; i < nodes.getLength(); i++) {
Element e = (Element)nodes.item(i);
Map<String, Object> datum = new HashMap<String, Object>(2);
String img_url = getValue(e, "pic");
URL url = null;
try {
url = new URL(img_url);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Bitmap bmp = null;
try {
bmp=BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
datum.put("imgurl", bmp);
datum.put("title", getValue(e, "title"));
datum.put("date", getValue(e, "description"));
datum.put("ex", "Autor: "+getValue(e, "creator")+" · Datum: "+getValue(e, "pubDate"));
datum.put("id", getValue(e, "id"));
data.add(datum);
}
final ListView lv = (ListView)findViewById(R.id.listView1);
ada = new SimpleAdapter(getApplicationContext(), data, R.layout.vlistimage, new String[] {"imgurl", "title", "date", "ex", "id"}, new int[] {R.id.list_image, android.R.id.text1, android.R.id.text2, R.id.text3});
lv.setAdapter(ada);
Thanks for a hint / helping! 感谢您的提示/帮助!
This is very common question and you could google example. 这是一个非常常见的问题,您可以使用google示例。 What I can tell you for sure it is WRONG to store your images in memory, as you can get OutOfMemoryException . 我可以肯定地告诉您,将图像存储在内存中是错误的 ,因为您可以获得OutOfMemoryException 。
To learn how to work with images and cache visit Displaying Bitmaps Efficiently 要了解如何使用图像和缓存,请访问有效显示位图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.