How to display images coming from webservice in android -
we working on project data coming webservice.i dispalying data in listview.the data icludes images image path.i have displayed iformation couldt display image.how display images webservices.
my code is:
package com.example.doctreachapp; import java.util.arraylist; import java.util.hashmap; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import com.example.doctreachapp.jsonparser; import com.example.doctreachapp.r; import android.app.listactivity; import android.app.progressdialog; import android.content.context; import android.graphics.bitmap; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.webkit.webview; import android.widget.baseadapter; import android.widget.imageview; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; public class generalhospitals extends listactivity { private context context; private static string url = "my url"; // private static final string id = "id"; string tag_user_detail = "dentalhospitals"; private static final string img = "img"; private static final string location = "location"; private static final string url = "url"; private static final string name = "name"; private static final string distance = "distance"; string imagestack; bitmap image; jsonarray user_detail_jsonarray = null; arraylist<hashmap<string, string>> jsonlist = new arraylist<hashmap<string, string>>(); listview lv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main2); lv = (listview) findviewbyid(android.r.id.list); new progresstask().execute(); } private class progresstask extends asynctask<string, void, boolean> { @override protected void onpostexecute(final boolean success) { setlistview(); // select single listview item // lv = getlistview(); } protected boolean doinbackground(final string... args) { jsonparser jparser = new jsonparser(); // json data url // jsonobject jobject = new jsonobject(result); jsonobject json = jparser.getjsonfromurl(url); log.d("json", json.tostring()); try { user_detail_jsonarray = json.getjsonarray("dentalhospitals"); log.d("jsonarray", user_detail_jsonarray.tostring()); } catch (jsonexception e1) { // todo auto-generated catch block e1.printstacktrace(); } (int = 0; < user_detail_jsonarray.length(); i++) { try { jsonobject c = user_detail_jsonarray.getjsonobject(i); string img = c.getstring(img); string name = c.getstring(name); string loc = c.getstring(location); string url = c.getstring(url); string dis = c.getstring(distance); hashmap<string, string> map = new hashmap<string, string>(); // add child node hashmap key & value map.put(img, img); map.put(name, name); map.put(location, loc); map.put(url, url); map.put(distance, dis); jsonlist.add(map); // log.d("list",jsonlist); } catch (jsonexception e) { e.printstacktrace(); } log.d("jsonlist", jsonlist.tostring()); } return true; } } public class customlistadapter extends baseadapter { private arraylist<hashmap<string, string>> listdata; private layoutinflater layoutinflater; public customlistadapter(context context, arraylist<hashmap<string, string>> jsonlist) { this.listdata = jsonlist; layoutinflater = layoutinflater.from(context); } @override public int getcount() { return listdata.size(); } @override public object getitem(int position) { return listdata.get(position); } @override public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = layoutinflater.inflate(r.layout.list_item, null); holder = new viewholder(); holder.name = (textview) convertview .findviewbyid(r.id.name); holder.location = (textview) convertview .findviewbyid(r.id.loc); holder.url = (textview) convertview .findviewbyid(r.id.url); holder.distance = (textview) convertview .findviewbyid(r.id.dis); holder.img = (imageview) convertview .findviewbyid(r.id.image); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } //system.out.println(listdata.get(position).get("img")); system.out.println(listdata.get(position).get("name")); holder.img.setimageuri(listdata.get(position).get("img")); holder.name.settext(listdata.get(position).get("name")); holder.location.settext(listdata.get(position).get("location")); holder.url.settext(listdata.get(position).get("url")); holder.distance.settext(listdata.get(position).get("distance")); //webview image = holder.img; //imagestack = listdata.get(position).get("img"); //image = bitmapfactory.decodestream((listdata.get(position).get("img")).openstream()); log.d("image",imagestack); return convertview; } } public static class viewholder { imageview img; textview location; textview name; textview url; textview distance; } public void setlistview() { // todo auto-generated method stub customlistadapter adapter = new customlistadapter(this, jsonlist); log.d("abcd", "" + lv.getvisibility()); lv.setadapter(adapter); } } please me.
write below code in getview()
have initialized holder.img
imageview
new downloadimagetask(holder.img) .execute(listdata.get(position).get("img"));
below asynctask
download image , display in imageview
private class downloadimagetask extends asynctask<string, void, bitmap> { imageview bmimage; public downloadimagetask(imageview bmimage) { this.bmimage = bmimage; } @override protected void onpreexecute() { super.onpreexecute(); } protected bitmap doinbackground(string... urls) { string urldisplay = urls[0]; bitmap micon11 = null; try { inputstream in = new java.net.url(urldisplay).openstream(); micon11 = bitmapfactory.decodestream(in); } catch (exception e) { log.e("error", e.getmessage()); e.printstacktrace(); } return micon11; } protected void onpostexecute(bitmap result) { bmimage.setimagebitmap(result); } }
if have small number of items in listview
use above code else recommendable use lazylist .
Comments
Post a Comment