I am trying to add downloaded images to a ListView
. I start out by getting the names of the image files from my server and then I do an AsyncTask
of building the bitmaps and storing them in a Bitmap[]
array. Then I use a HashMap
to add the data using the SimpleAdapter
shown below. I want to know how to add the bitmap images to the list view.
Right now I am getting an error on the line where I attempt to put the item photo into the hash map. How do I resolve that? Do I have to move them to the drawable
resources directory? I am totally new to this and will appreciate any help in getting downloaded images into a list view. Here is how I prepare the data. All of this is taking place in the onPostExecute()
method of my async task in which OI perform another async task to get the image files.
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);
Here is where I get the downloaded images:
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++;
}
}