Android adapter for a custom layout listview based on a class -
i have class looks this:
public class vizita { public int icon; public string titlu; public string loc; public string idviz; public vizita(){ super(); } public vizita(int icon, string titlu, string loc, string idviz) { super(); this.icon = icon; this.titlu = titlu; this.loc = loc; this.idviz = idviz; } }
i use asynctask retrieve json array php. works perfect simple layout of listview. want use more complex layout, designed 1 uses 4 values vizita.class above. not know how set adapter it, because way try it, gives me errors.
of course trying in onpostexecute, this:
public class myactivity extends activity { {...} jsonarray lststat; {...} public jsonarray liststatus() { jsonarray jdata=post.getserverdatax(url_connectx); return jdata; } class asyncpost extends asynctask< string, string, string > { vizita weather_data[]; .... protected void onpostexecute(string result) { vizitaadapter adapter = new vizitaadapter(myactivity.this,r.layout.listview_item_row, weather_data); mylistview.setadapter(adapter); ...}
and in doinbackground processing json this:
lststat = liststatus(); jsonobject json_data; try { int length = lststat.length(); vizita weather_data[] = new vizita[length]; (int = 0; < length; i++) { json_data = lststat.getjsonobject(i); weather_data[i].titlu =json_data.getstring("clientnume"); weather_data[i].idviz =json_data.getstring("idvizite"); weather_data[i].loc =json_data.getstring("localitate"); } } catch (exception e) { log.e(null, e.tostring()); }
where liststatus() call asynctask retrieve needed jsonarray.
but receive java.lang.nullpointerexception in loop (if comment setadapter line).
i assume did not initialize weather_data array ?!?!? if (uncomment) set adapter in onpostexecute receive crash error "force close" not receive errors in ide, seeming syntaxes fine.
please tell me doing wrong? , how can fix it?
the code vizitaadapter bellow
public class vizitaadapter extends arrayadapter<vizita>{ context context; int layoutresourceid; vizita data[] = null; public vizitaadapter(context context, int layoutresourceid, vizita[] data) { super(context, layoutresourceid, data); this.layoutresourceid = layoutresourceid; this.context = context; this.data = data; } @override public view getview(int position, view convertview, viewgroup parent) { view row = convertview; weatherholder holder = null; if(row == null) { layoutinflater inflater = ((activity)context).getlayoutinflater(); row = inflater.inflate(layoutresourceid, parent, false); holder = new weatherholder(); holder.imgicon = (imageview)row.findviewbyid(r.id.imgicon); holder.txttitle = (textview)row.findviewbyid(r.id.txttitle); holder.txtloco = (textview)row.findviewbyid(r.id.txtlocalitate); holder.txtidviz = (textview)row.findviewbyid(r.id.txtidvizite); row.settag(holder); } else { holder = (weatherholder)row.gettag(); } vizita weather = data[position]; holder.txttitle.settext(weather.titlu); holder.txtloco.settext(weather.loc); holder.txtidviz.settext(weather.idviz); holder.imgicon.setimageresource(weather.icon); return row; } static class weatherholder { imageview imgicon; textview txttitle; textview txtloco; textview txtidviz; } }
probably forgot initialize vizita
object in loop:
json_data = lststat.getjsonobject(i); weather_data[i] = new vizita(); weather_data[i].titlu =json_data.getstring("clientnume"); weather_data[i].idviz =json_data.getstring("idvizite"); weather_data[i].loc =json_data.getstring("localitate");
Comments
Post a Comment