android - Populating GridView from JSON using Async Task -
i trying populate grid view json received server. need populate gridview results obtained. below activity file populating grid view
public class opentableactivity extends activity { string seruri, method; opentableadapter op; arraylist<hashmap<string, string>> tablelist = new arraylist<hashmap<string, string>>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.open_table); linearlayout layout = (linearlayout) findviewbyid(r.id.opentablelinear); layout.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view view, motionevent ev) { hidekeyboard(view); return false; } }); gettables(); gridview gridview = (gridview) findviewbyid(r.id.grid); gridview.setadapter(new opentableadapter(this)); button btn = (button) findviewbyid(r.id.table_home_btn); btn.setonclicklistener(homebtn); } public boolean gettables() { seruri = "tables.json"; method = "get"; webserviceasynctask webservicetask = new webserviceasynctask(opentableactivity.this); webservicetask.execute(seruri, method, this); return true; } public void writejsonarray(final jsonarray result, context contextinstance) { try { (int = 0; < result.length(); i++) { jsonobject c = result.getjsonobject(i); string tablabel = c.getstring("tablabel"); string tabstatus = c.getstring("tabstatus"); hashmap<string, string> map = new hashmap<string, string>(); map.put("table_name", tablabel); map.put("table_status", tabstatus); tablelist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } }
below adapter class grid view
public class opentableadapter extends baseadapter { private context mcontext; string ordernumber; opentableactivity openinstance; string tablelabel; string tablestatus; //arraylist<hashmap<string, string>> tablist = null; public opentableadapter(context c) { // todo auto-generated constructor stub mcontext = c; //tablist = tablelist; } @override public int getcount() { // todo auto-generated method stub openinstance = new opentableactivity(); return openinstance.tablelist.size(); } @override public object getitem(int arg0) { // todo auto-generated method stub return null; } @override public long getitemid(int arg0) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { view view = null; if (convertview == null) { layoutinflater li = (layoutinflater) mcontext .getsystemservice(context.layout_inflater_service); view = li.inflate(r.layout.button, null); } else { view = convertview; } button btn = (button) view.findviewbyid(r.id.button); openinstance = new opentableactivity(); (hashmap<string, string> map : openinstance.tablelist) (java.util.map.entry<string, string> mapentry : map.entryset()) { string key = mapentry.getkey(); string value = mapentry.getvalue(); btn.settext(key);
the tablelist populated correctly in writejsonarray. control goes adapter class gridview.setadapter(new opentableadapter(this));
. executes nothing there. had put breakpoints see actual flow comes below method , nothing executed thereafter. there no error in trace , it's not executing anything.
public opentableadapter(context c) { // todo auto-generated constructor stub mcontext = c; }
i believe due incorrect context passed when comes async task 'post execute' method. tried passing context still same issue. please advise. thanks.
you have not initialized gridview
gridview gridview = (gridview) findviewbyid(r.id.gridview);
you have edited post , have gridview initialized that's not problem.
initialize minflater in constructor of baseadapter class.also pass hashmap construcotr , use same in getview.
what doing in getview wrong openinstance = new opentableactivity()
wrong. can start activity calling startactivity
.
layoutinflater minflater; public opentableadapter(context c) { mcontext = c; minflater = layoutinflater.from(c); tablist = tablelist; }
in getview
viewholder holder; if (convertview == null) { convertview = minflater.inflate(r.layout.button, parent, false); holder = new viewholder(); holder.btn = (button) convertview.findviewbyid(r.id.button); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } // use tablelist data set text button using position index hashmap<string, string>> map = tablist.get(position) // use map set text button now. holder.btn.settext(key); return convertview; }
use viewholder. http://developer.android.com/training/improving-layouts/smooth-scrolling.html
static class viewholder { button btn; }
Comments
Post a Comment