1

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.

Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50
Arash
  • 11
  • 2
  • You have to implement your own adapter class. – OneCricketeer Dec 17 '16 at 18:14
  • how can i do this ? @cricket_007 – Arash Dec 17 '16 at 18:15
  • You lookup how to write a custom adapter. But, that isn't the only problem. `new URL(imageUrl).getContent()` needs to happen in the background. You can't download images just like that - the app will crash – OneCricketeer Dec 17 '16 at 18:16
  • I would suggest using the [Picasso library](https://square.github.io/picasso/) anyway, but you can see here http://stackoverflow.com/questions/33328190/picasso-with-arrayadapter and here http://stackoverflow.com/questions/23120238/using-picasso-library-with-listview for references for how your code would look like. – OneCricketeer Dec 17 '16 at 18:19
  • thank you my friend but i want to code my self ( without any external lib ) – Arash Dec 17 '16 at 18:32
  • is there other way to merge them ? @cricket_007 – Arash Dec 17 '16 at 18:33
  • `ArrayAdapter` is not an external lib. That is all you need. If you want images, that is a different problem. You say *"I can get them by this code"* - but unless that code is in an AsyncTask, you cannot get that Bitmap – OneCricketeer Dec 17 '16 at 19:16
  • Check this out http://stackoverflow.com/questions/23120238/using-picasso-library-with-listview?noredirect=1&lq=1 – Athul Dec 17 '16 at 19:43

0 Answers0