2011-06-10 79 views
5

我想在ListView中顯示新聞(來自RSS)。我已經成功創建我的ListView,正確顯示標題和說明。但是我無法顯示來自網址的圖片。在ListView中顯示來自url的可繪製圖像

這裏是我的代碼:

maListViewPerso = (ListView) findViewById(R.id.listviewperso); 
ArrayList <HashMap<String, String>> listItem = new ArrayList<HashMap<String, 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); 

正如您可以猜到,newsArray[i+4]包含我的圖像的URL。

我寫了一個函數drawable_from_url,它返回一個android.graphics.drawable.Drawable,它工作正常。我試圖寫這個:

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

但它不工作(無圖像顯示)。的 String.valueOf(drawable_from_url(newsArray[i+4], "newsPic"))
價值的一個例子是
[email protected]

任何想法?

謝謝。

回答

0

試試這個方法從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; 
    } 
} 
相關問題