android - Nullpointerexception while updating UI -
i'm reading records database , loading them in listview
. listview
consist checkbox
, textview
. loading done on asynctask
. part of application works fine.
the next step automatically checking checkboxes according flags database , here problem. i'm trying check items inside onpostexecute()
, error nullpointerexception
. if same from, example, setonclicklistener()
of button widget works fine.
the question how check if listview
populated, checkboxes , textview
loaded , visible on screen?
i don't know if part of code program breaks looks like:
(j=0; j<3; j++) { linearlayout itemlayout = (linearlayout)listview.getchildat(j); // find under linearlayout checkbox checkbox = (checkbox)itemlayout.findviewbyid(r.id.colchk); (k=0; k<rbmjere.size(); k++) { if (checkbox.gettag().tostring() == rbmjere.get(k).tostring()) { checkbox.setchecked(true); } } }
it breaks on line:
linearlayout itemlayout = (linearlayout)listview.getchildat(j);
i must mention works if manually press on button runs code automatic checking boxes , works when data loaded.
here code:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_spckontrola_update); listview = (listview)findviewbyid(r.id.listview1); btnpohrani = (button)findviewbyid(r.id.btnpohrani); btnprovjeri = (button)findviewbyid(r.id.btnprovjeri); btnprovjeri.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { new loadspckontrole().execute("fcn"); } }); myarrlist = new arraylist<hashmap<string, string>>(); public void filldata() { simpleadapter listadapter = new simpleadapter(this, myarrlist, r.layout.activity_list_row, new string[] {"opismjere", "rbmjere"}, new int[] {r.id.colopis, r.id.colcode}); listview = (listview)findviewbyid(r.id.listview1); listview.setadapter(listadapter); } private class loadspckontrole extends asynctask<string, void, void> { protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(spcupdate.this); pdialog.setmessage("loading in progress ..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } @override protected void doinbackground(string... params) { hashmap<string, string> map; string k = params[0].tostring(); arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("sidkategorija", k)); try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://192.168.16.48" + "/spc/get_spcmjere.php"); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } catch(exception e) { log.e("log_tag", "error in http connection "+ e.tostring()); } try { bufferedreader reader = new bufferedreader(new inputstreamreader(is,"utf-8"),8); sb = new stringbuilder(); sb.append(reader.readline() + "\n"); string line="0"; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result=sb.tostring(); } catch(exception e) { log.e("log_tag", "error converting result " + e.tostring()); } int ct_id; string ct_name; try { jarray = new jsonarray(result); jsonobject json_data=null; for(int i=0;i<jarray.length();i++){ json_data = jarray.getjsonobject(i); ct_id=json_data.getint("rbmjere"); ct_name=json_data.getstring("opismjere"); map = new hashmap<string, string>(); map.put("rbmjere", string.valueof(ct_id)); map.put("opismjere", ct_name); myarrlist.add(map); } } catch(jsonexception e1) { log.e("greška konvertiranja", e1.tostring()); } catch (parseexception e1) { e1.printstacktrace(); } return null; } protected void onpostexecute(void result) { super.onpostexecute(result); filldata(); listview.setadapter(new spcmjereadapter(spcupdate.this)); pdialog.dismiss(); } } public class spcmjereadapter extends baseadapter { private context context; public spcmjereadapter(context c) { context = c; } public int getcount() { return myarrlist.size(); } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(final int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); if (convertview == null) { convertview = inflater.inflate(r.layout.activity_list_row, null); } // colid textview txtopis = (textview) convertview.findviewbyid(r.id.colopis); txtopis.settext(myarrlist.get(position).get("opismjere") +"."); // colcode textview txtrbmjere = (textview) convertview.findviewbyid(r.id.colcode); txtrbmjere.settext(myarrlist.get(position).get("rbmjere")); // colchk checkbox chk = (checkbox) convertview.findviewbyid(r.id.colchk); chk.settag(myarrlist.get(position).get("rbmjere")); return convertview; } }
and here code how items should checked:
db objdb = new db(); arraylist<integer> rbmjere = objdb.getcheckedspc(id); int k=0; int j=0; (j=0; j<myarrlist.size(); j++) { linearlayout itemlayout = (linearlayout)listview.getchildat(j); // find under linearlayout checkbox checkbox = (checkbox)itemlayout.findviewbyid(r.id.colchk); (k=0; k<rbmjere.size(); k++) { if (checkbox.gettag().tostring() == rbmjere.get(k).tostring()) { checkbox.setchecked(true); } } }
this code above works when listview populated items , if code under onclicklistener() doesn't work if run onpostexecute because seems rows in listview not loaded. question should information when loading of rows finished , after check items should checked according data database?
because of view recycling, listview.getchildat() return view positions displaying, not severals.
you can check other question here. has answer looking for: listview getchildat returning null visible children
Comments
Post a Comment