Search code examples
androidandroid-listviewsimpleadapter

Drawable is not set in ListView


I having a list of Bitmap containing bitmaps retrieved from different URLs.

@Override
protected List<Bitmap> doInBackground(Void... params) {
    for (int i=0;i<payss.size();i++){
        try {
            list_drapeaux.add(BitmapFactory.decodeStream((InputStream)new URL(payss.get(i).getLogo()).getContent()));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return list_drapeaux;
}

payss.get(i).getLogo()) containing different images URLs.

I'm using SimpleAdapter to set that images in a ListView:

final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i=0;i<payss.size();i++){
    Map<String,Object> map = new HashMap<String,Object>();
    Bitmap contact_pic = pays_drapeaux.get(i);   
    drawable = new BitmapDrawable(contact_pic); 
    map.put("pays", payss.get(i).getNom());
    map.put("drapeau",contact_pic);
    Log.i("PAYS",payss.get(i).getNom());
    list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(fa, (List<? extends Map<String, ?>>) list,
    R.layout.pays_item, new String[] {"drapeau","pays"},
    new int[] { R.id.image,R.id.text_pays});
setListAdapter(adapter);

I'm converting the Bitmap to Drawable and add it to map then list.


Solution

  • i would suggest using a BaseAdapter instead of a SimpleAdater as it gives you more flexibility over what and how to show the data , and set the images via the getView method (watch this video for more information) .

    also , i would suggest to never store bitmaps in such a manner since you don't know how much memory they use compared to the available memory . use a caching mechanism instead (read here for more information ) .