android - Parsing JSONArray and populating it in a ListView -
following activity
supposed take values (users) json, , put them in arraylist<user>
user
class created. after doing supposed fill listview
these values. problem every time try open activity, crashes.
can please tell me problem? , class works purpose want?
public class search extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_search); } public void onclick(view v) { switch (v.getid()){ case r.id.imagebutton2: new storeuserdataasynctask().execute(); break; } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_searchh, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } arraylist<user> userlist1=null; public class storeuserdataasynctask extends asynctask<void,void,arraylist<user>> { user user; getusercallback usercallback; progressdialog progressdialog; spinner s11 = (spinner) findviewbyid(r.id.spinner); spinner s2 = (spinner) findviewbyid(r.id.spinner2); string bloodd = s11.getselecteditem().tostring(); string loc = s2.getselecteditem().tostring(); protected arraylist<user> doinbackground(void... params) { user returneduser=null; arraylist<user> userlist=null; final int connection_timeout = 1000 * 15; final string server_address = "http://xxxxx.comli.com/"; arraylist<namevaluepair> datatosend = new arraylist<>(); datatosend.add(new basicnamevaluepair("bloodtype", bloodd)); datatosend.add(new basicnamevaluepair("location", loc)); httpparams httprequestparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httprequestparams, connection_timeout); httpconnectionparams.setsotimeout(httprequestparams, connection_timeout); httpclient client = new defaulthttpclient(httprequestparams); httppost post = new httppost(server_address + "/search.php"); try { post.setentity(new urlencodedformentity(datatosend)); httpresponse httpresponse = client.execute(post); httpentity entity = httpresponse.getentity(); string result = entityutils.tostring(entity); jsonobject jobject = new jsonobject(result); if (jobject.length() == 0) { userlist = null; context context = getapplicationcontext(); charsequence text = "no available donor!"; int duration = toast.length_short; } else { string resultt = entityutils.tostring(entity); jsonarray jsonarray = new jsonarray(resultt); (int = 0; < jsonarray.length(); i++) { jsonobject jsonobject = jsonarray.getjsonobject(i); string fname = jsonobject.getstring("firstname"); string lname = jsonobject.getstring("lastname"); string blood = jsonobject.getstring("blood"); string location = jsonobject.getstring("location"); string email = jsonobject.getstring("email"); string age = jsonobject.getstring("age"); string password = jsonobject.getstring("password"); string phone = jsonobject.getstring("phone"); int agee = integer.parseint(age); int phonee = integer.parseint(phone); returneduser = new user(fname, lname, blood, location, email, password, agee, phonee); userlist.add(returneduser); } } } catch (exception e) { e.printstacktrace(); } userlist1=userlist; return userlist; } protected void onpostexecute(arraylist<user> avoid) { progressdialog.dismiss(); displaycountrylist(); } } private void displaycountrylist(){ mycustomadapter dataadapter = null; try { //create arrayadaptar string array dataadapter = new mycustomadapter(this, r.layout.listv, userlist1); listview listview = (listview) findviewbyid(r.id.listview); // assign adapter listview listview.setadapter(dataadapter); //enables filtering contents of given listview listview.settextfilterenabled(true); } catch (exception e) { e.printstacktrace(); } } class mycustomadapter extends arrayadapter<user> { private arraylist<user> userlist; public mycustomadapter(context context, int textviewresourceid, arraylist<user> userlist) { super(context, textviewresourceid, userlist); this.userlist = new arraylist<user>(); this.userlist.addall(userlist); } private class viewholder { textview name; textview phone; textview email; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; log.v("convertview", string.valueof(position)); if (convertview == null) { layoutinflater vi = (layoutinflater)getsystemservice( context.layout_inflater_service); convertview = vi.inflate(r.layout.listv, null); holder = new viewholder(); holder.name = (textview) convertview.findviewbyid(r.id.textview31); holder.phone = (textview) convertview.findviewbyid(r.id.textview33); holder.email = (textview) convertview.findviewbyid(r.id.textview34); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } user user = userlist.get(position); holder.name.settext(user.getfirstname()); holder.phone.settext(user.getphone()); holder.email.settext(user.getemail()); return convertview; } } }
Comments
Post a Comment