android - how to add volley image request in an arraylist? -
i have below code 2 things , volley
request set image , , array list adapter
show static images.
i need add volley request in array list , want volley images shown in arraylist
how can me please?
(note have phpscript send me in json path url of images)
the code :main_activity
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); requestqueue mrequestqueue; final string image_url = "http://developer.android.com/images/training/system-ui.png"; mnetworkimageview = (networkimageview) findviewbyid(r.id.networkimageview); //instantiate cache cache cache = new diskbasedcache(getcachedir(), 1024 * 1024); // 1mb cap //set network use httpurlconnection http client. network network = new basicnetwork(new hurlstack()); //instantiate requestqueue cache , network. mrequestqueue = new requestqueue(cache, network); //start queue mrequestqueue.start(); imageloader mimageloader = new imageloader(mrequestqueue, new lrubitmapcache( lrubitmapcache.getcachesize(mcontext))); mnetworkimageview.setimageurl(image_url, mimageloader); customlistadapter adapter=new customlistadapter(this, itemname, imgid); list=(listview)findviewbyid(r.id.list); list.setadapter(adapter); list.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub string slecteditem= itemname[+position]; toast.maketext(getapplicationcontext(), slecteditem, toast.length_short).show(); } }); }
arraylistadapter
package com.example.p;
import android.app.activity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; public class customlistadapter extends arrayadapter<string> { private final activity context; private final string[] itemname; private final integer[] imgid; public customlistadapter(activity context, string[] itemname, integer[] imgid) { super(context, r.layout.mylist, itemname); // todo auto-generated constructor stub this.context=context; this.itemname=itemname; this.imgid=imgid; } public view getview(int position,view view,viewgroup parent) { layoutinflater inflater=context.getlayoutinflater(); view rowview=inflater.inflate(r.layout.mylist, null,true); textview txttitle = (textview) rowview.findviewbyid(r.id.item); imageview imageview = (imageview) rowview.findviewbyid(r.id.icon); textview extratxt = (textview) rowview.findviewbyid(r.id.textview1); txttitle.settext(itemname[position]); imageview.setimageresource(imgid[position]); extratxt.settext("description "+itemname[position]); return rowview; }; }
and lrubitmapcache
package com.example.p; import android.content.context; import android.graphics.bitmap; import android.support.v4.util.lrucache; import android.util.displaymetrics; import com.android.volley.toolbox.imageloader.imagecache; public class lrubitmapcache extends lrucache<string, bitmap> implements imagecache { public lrubitmapcache(int maxsize) { super(maxsize); } public lrubitmapcache(context ctx) { this(getcachesize(ctx)); } @override protected int sizeof(string key, bitmap value) { return value.getrowbytes() * value.getheight(); } @override public bitmap getbitmap(string url) { return get(url); } @override public void putbitmap(string url, bitmap bitmap) { put(url, bitmap); } // returns cache size equal approximately 3 screens worth of images. public static int getcachesize(context ctx) { final displaymetrics displaymetrics = ctx.getresources(). getdisplaymetrics(); final int screenwidth = displaymetrics.widthpixels; final int screenheight = displaymetrics.heightpixels; // 4 bytes per pixel final int screenbytes = screenwidth * screenheight * 4; return screenbytes * 3; } }
in other post ask me help. image comes rest server json string base64 coded. next:
public view getview(int position, view convertview, viewgroup parent) { final objectlogados o = items.get(position); final viewholder holder; view vi = convertview; if (vi == null){ layoutinflater inflater = context.getlayoutinflater(); vi = inflater.inflate(r.layout.singlerowlog, null);// e' preciso fazer o inflate pois view nao ainda nao esta em activity alguma holder = new viewholder(); holder.ivimgmedlog= (imageview) vi.findviewbyid(r.id.ivp_imagemedlog) ; holder.tv_usuario = (textview) vi.findviewbyid(r.id.tv_hnomemedlogado); vi.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.tv_usuario.settext(o.getusuario()); string imagekey="med"+string.valueof(o.getnr_usuario()); final bitmap bitmap = common.getinstance().getbitmapfrommemcache(imagekey); if (bitmap != null) { holder.ivimgmedlog.setimagebitmap(bitmap); }else { holder.ivimgmedlog.setimagedrawable(context.getresources() .getdrawable(r.drawable.empty)); downloadimage(holder.ivimgmedlog,o.getnr_usuario()); } return vi; } static class viewholder { textview tv_usuario; imageview ivimgmedlog; } private void downloadimage(final imageview imageview,final int nr){ //final imageview imageview=imv; string url=common.getserverurl() + "\"retrieveimagestaff\""; jsonarray ja = new jsonarray(); ja.put(nr); jsonobject jsonbody = new jsonobject(); try { jsonbody.put("_parameters", ja); } catch (jsonexception e) { e.printstacktrace(); } jsonobjectrequest jr = new jsonobjectrequest(request.method.post, url, jsonbody, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { jsonobject anentry = null, entries = null; string ims = ""; try { // todo auto-generated method stub jsonarray lresult = response.getjsonarray("result"); string s = lresult.get(0).tostring(); anentry = new jsonobject(s); ims = anentry.getstring("imagem"); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } bitmap decodedbyte = null; try { if (!textutils.isempty(ims)) { byte[] decodedstring = base64.decode(ims, base64.default); decodedbyte = bitmapfactory.decodebytearray(decodedstring, 0, decodedstring.length); if (imageview != null) { if (decodedbyte != null) { imageview.setimagebitmap(decodedbyte); common.getinstance().addbitmaptomemorycache("med"+string.valueof(nr), decodedbyte); } else { imageview.setimagedrawable(imageview.getcontext().getresources() .getdrawable(r.drawable.empty)); } } } else { imageview.setimagedrawable(imageview.getcontext().getresources() .getdrawable(r.drawable.empty)); } }catch (exception e){ e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { // todo auto-generated method stub //log.v(tag, error.getmessage()); toast.maketext(context, "erro adquirindo imagem...." + error.getmessage(), toast.length_short).show(); error.printstacktrace(); } }); jr.setretrypolicy(new defaultretrypolicy(common.default_timeout_ms, common.default_max_retries, defaultretrypolicy.default_backoff_mult)); common.getinstance().addtoreqqueue(jr, "jreq"); //reqqueue.add(jr); }
you can change request use volley , image. so, can change code described in https://developer.android.com/training/volley/request.html#request-image similar to:
imageview mimageview; string url = "http://i.imgur.com/7spzg.png"; mimageview = (imageview) findviewbyid(r.id.myimage); ... // retrieves image specified url, displays in ui. imagerequest request = new imagerequest(url, new response.listener<bitmap>() { @override public void onresponse(bitmap bitmap) { mimageview.setimagebitmap(bitmap); } }, 0, 0, null, new response.errorlistener() { public void onerrorresponse(volleyerror error) { mimageview.setimageresource(r.drawable.image_load_error); } }); // access requestqueue through singleton class. mysingleton.getinstance(this).addtorequestqueue(request);
Comments
Post a Comment