arrays - For each key, show value - Android -
im making app site, , need making statement. parse json api (server) , catch it, working, want show in listview, i've made adapter , that, working. when launch app 1 line in listview shows. have no idea on how values listview.
my activity:
public class filesactivity extends sherlockactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.dblist); getsupportactionbar().setdisplayhomeasupenabled(true); getsupportactionbar().settitle("files"); string response = null; defaulthttpclient httpclient = new defaulthttpclient(); responsehandler <string> resonsehandler = new basicresponsehandler(); httppost postmethod = new httppost("http://api.bayfiles.net/v1/account/files?session=<sessionid>"); try { jsonobject json = new jsonobject(); postmethod.setentity(new bytearrayentity(json.tostring().getbytes("utf8"))); postmethod.setheader( "content-type", "application/json" ); response = httpclient.execute(postmethod,resonsehandler); jsonobject request = new jsonobject(response); (iterator<?> keyiterator = request.keys(); keyiterator.hasnext(); ) { string key = (string) keyiterator.next(); jsonobject object = request.optjsonobject(key); arraylist<fileobject> objectlist = new arraylist<fileobject>(); //arraylist<fileobject> results = new arraylist<fileobject>(); if (object != null) { fileobject obj = new fileobject(); obj.setfileid(key); obj.setfilename(object.getstring("filename")); obj.setsize(object.getstring("size")); obj.setinfotoken(object.getstring("infotoken")); obj.setdeletetoken(object.getstring("deletetoken")); obj.setsha1(object.getstring("sha1")); objectlist.add(obj); log.d("log_tag", object.getstring("filename")); } final listview lv1 = (listview) findviewbyid(r.id.listobjects); lv1.setadapter(new mycustombaseadapter(this, objectlist)); lv1.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> a, view v, int position, long id) { object o = lv1.getitematposition(position); fileobject fullobject = (fileobject)o; toast.maketext(filesactivity.this, "you have chosen: " + " " + fullobject.getfilename(), toast.length_long).show(); } }); } } catch(exception e) { e.printstacktrace(); log.d("log_tag", "error: " + e.tostring()); } } }
and adapter , fileobject standard, how can make listview show values?
see this: populating listview using arraylist?
you're re-creating arraylist every cycle through loop. it's ever going have 1 item in it. create arraylist prior for-loop, , populate inside loop. after you've done add whole arraylist listview. no need every single cycle. should like:
arraylist<fileobject> objectlist = new arraylist<fileobject>(); (iterator<?> keyiterator = request.keys(); keyiterator.hasnext(); ) { string key = (string) keyiterator.next(); jsonobject object = request.optjsonobject(key); //arraylist<fileobject> results = new arraylist<fileobject>(); if (object != null) { fileobject obj = new fileobject(); obj.setfileid(key); obj.setfilename(object.getstring("filename")); obj.setsize(object.getstring("size")); obj.setinfotoken(object.getstring("infotoken")); obj.setdeletetoken(object.getstring("deletetoken")); obj.setsha1(object.getstring("sha1")); objectlist.add(obj); log.d("log_tag", object.getstring("filename")); } }//end for-loop right here. no need other stuff on , over.
Comments
Post a Comment