Add Image To ListAdapter

241 views Asked by At

I have a SimpleAdapter as follow :

ListAdapter adapter = new SimpleAdapter(
                            ItemDetail.this, itemList,
                            R.layout.list_item, new String[]{
                            TAG_PID,
                            TAG_NAME,
                            TAG_PICTURE
                    },
                            new int[]{
                                    R.id.pid,
                                    R.id.name,
                                    R.id.img
                            });
setListAdapter(adapter);

But there is a problem for images. my images need to get from a web address. I can get them by this code:

try {
     final String imageUrl = "http://example.com/pic/jpg";
     InputStream in = (InputStream) new URL(imageUrl).getContent();
     Bitmap bitmap = BitmapFactory.decodeStream(in);
     in.close();
 } catch (Exception e) {
     e.printStackTrace();
 }

And finally set image:

ImageView image = (ImageView) findViewById(R.id.img);
//img is a single item in a list_item xml file
image.setImageBitmap(bitmap);

But my question is, how can I merge all of above code ?? download and set image for every item in the list.

0

There are 0 answers