我正在尝试将下载的图像添加到ListView
. 我首先从我的服务器获取图像文件的名称,然后我AsyncTask
构建位图并将它们存储在一个Bitmap[]
数组中。然后我使用 aHashMap
添加数据,SimpleAdapter
如下所示。我想知道如何将位图图像添加到列表视图。
现在我在尝试将项目照片放入哈希映射的行中遇到错误。我该如何解决?我必须将它们移动到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++;
}
}