开发者

Display drawable image from url in a ListView

I want to display news (from RSS) in a ListView. I already succeed to create my ListView, display correctly both title and description. But I can't display a image from URL.

Here is my code :

maListViewPerso = (ListView) findViewById(R.id.listviewperso);
ArrayList <HashMap<String, String>> listItem = new ArrayList<HashMap<String,开发者_如何学Go String>>();
HashMap<String, String> map;

int i = 0;
while (i < 55)
{
    map = new HashMap<String, String>();
    map.put("titre", newsArray[i]);
    map.put("description", newsArray[i+1]));
    map.put("img", newsArray[i+4]);
    listItem.add(map);

    i += 5;
}

SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, 
R.layout.affichageitem, new String[] {"img", "titre", "description"}, 
new int[] {R.id.img, R.id.titre, R.id.description});

maListViewPerso.setAdapter(mSchedule);

As you can guess, newsArray[i+4] contains the URL of my image.

I've written a function drawable_from_url which returns a android.graphics.drawable.Drawable and which is working fine. I tried to write this :

map.put("img", String.valueOf(drawable_from_url(newsArray[i+4], "newsPic")));

But it's not working either (no image displayed). An example of the value of String.valueOf(drawable_from_url(newsArray[i+4], "newsPic"))

could be

android.graphics.drawble.BitmapDrawable@481f16d0

Any idea ?

Thanks.


try this method to load bitmap from url

    public  Bitmap setRemoteImage(URL url) {
    try {
        Bitmap bm=null;
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis=new BufferedInputStream(is);
        try {
            bm = BitmapFactory.decodeStream(bis);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            MainActivity mm= new MainActivity();
            Drawable dra=mm.getResources().getDrawable(R.drawable.icon);
            Bitmap bb=((BitmapDrawable)dra).getBitmap();
            bm=bb;
        }
        is.close();
        return bm;
    }catch (IOException e) {
        return null;
    }   
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜