2013-03-14 90 views
0

我試圖將下載的圖像添加到ListView。我首先從我的服務器獲取圖像文件的名稱,然後執行構建位圖並將它們存儲在Bitmap[]陣列中的AsyncTask。然後我使用HashMap來添加使用下面顯示的SimpleAdapter的數據。我想知道如何將位圖圖像添加到列表視圖。如何將下載的圖像添加到ListView?

現在我正在嘗試將項目照片放入哈希映射的行上出現錯誤。我如何解決這個問題?我必須將它們移動到drawable資源目錄嗎?我完全不熟悉這一點,並會感謝將下載的圖像獲取到列表視圖中的任何幫助。這是我如何準備數據。所有這些都發生在我的異步任務的onPostExecute()方法中,其中OI執行另一個異步任務來獲取圖像文件。

new AccessImages().execute("http://10.0.2.2:8000/herbivorenew/" + viewdata[2]); 

List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); 

for (int i=0;i< count; i++) { 
    HashMap<String, String> hm = new HashMap<String,String>(); 
    hm.put("item_header", viewtext[i]); 
    // built from an asynctask from the server 
    hm.put("item_photo", (Bitmap)(itemphoto[i])); 
    hm.put("carrots", Integer.toString(carrotimage[i])); 

    aList.add(hm); 
} 

// Keys used in Hashmap 
String[] from = {"item_header", "item_photo", "carrots"}; 

// Ids of views in listview_layout 
int[] to = {R.id.item_header, R.id.item_photo, R.id.item_carrot}; 

// Instantiating an adapter to store each items 
// R.layout.listview_layout defines the layout of each item 
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), 
aList, R.layout.itemlist, from, to); 

// Getting a reference to listview of main.xml layout file 
ListView item_list = (ListView) findViewById(R.id.listitems); 
item_list.setAdapter(adapter); 

這裏就是我得到的下載的圖片:

private class AccessImages extends AsyncTask<String, Void, Bitmap> { 

    protected Bitmap doInBackground(String... urladds){ 
     return downloadImage(urladds[0]); 
    } 

    protected void onPostExecute(Bitmap bmp) { 
     itemphoto[itemcount] = bmp; 
     itemcount++; 
    } 
} 
+0

希望它能幫助:http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview – gZerone 2013-03-14 01:26:39

回答