1

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. 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!

Community
  • 1
  • 1
user1756209
  • 573
  • 10
  • 23

1 Answers1

1

This is very common question and you could google example. What I can tell you for sure it is WRONG to store your images in memory, as you can get OutOfMemoryException.

To learn how to work with images and cache visit Displaying Bitmaps Efficiently

Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68
  • Yeah Handling Images in android is something difficult You need to download the images in background.It is too much headache for main thread to process your request.You may definitely get OutOfmemory – Chaitu Nov 03 '12 at 18:29
  • OK, I tried to overwrite SimpleAdapter. I've another question about this method: http://stackoverflow.com/questions/13218457/how-could-i-use-setviewimage-in-simpleadapter Thanks! – user1756209 Nov 04 '12 at 12:21