OnItemSelectedListener method in Spinner is not called when i set JSON data to Spinner Android -
when select static data spinner, onitemselectedlistener
method works fine, when add json data spinner, populates json data , onitemselectedlistener
not getting called. problem? here code:
class list_activity extends activity implements onitemselectedlistener { progressdialog progressdialog; requestqueue requestqueue; string json_id; string json_dept; list<string> spinner_dept; list<string> categories; string selectedcity; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list_); progressdialog = new progressdialog(this); progressdialog.setmessage("loading"); progressdialog.show(); // spinner element spinner spinner = (spinner) findviewbyid(r.id.spinner3); requestqueue= volley.newrequestqueue(this); // spinner click listener spinner.setonitemselectedlistener(this); // spinner drop down elements spinner_dept = new arraylist<string>(); categories = new arraylist<string>(); categories.add("automobile"); categories.add("business services"); categories.add("computers"); categories.add("education"); categories.add("personal"); categories.add("travel"); jsonobjectrequest jsonobjectrequest = new jsonobjectrequest( request.method.get, "http://192.168.1.104/leave/public/api/department", null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { progressdialog.dismiss(); try { jsonarray jsonarray = response.getjsonarray("data"); (int = 0; < jsonarray.length(); i++) { jsonobject jsonobject = jsonarray.getjsonobject(i); string b = jsonarray.getjsonobject(i).getstring("department"); spinner_dept.add(b); json_id = jsonobject.getstring("identity"); json_dept = jsonobject.getstring("department"); department deprt = new department(); deprt.setid(json_id); deprt.setdepartment(json_dept); // spinner_data.add(deprt); // spinner_dept.add(jsonobject.getstring("department")); //textview.append( id+" "+ first_name + " " + last_name + "\n"); } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.d("error", "error server response"); } }); requestqueue.add(jsonobjectrequest); // creating adapter spinner arrayadapter<string> dataadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, spinner_dept); // drop down layout style - list view radio button dataadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); // attaching data adapter spinner spinner.setadapter(dataadapter); } @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { // on selecting spinner item string item = parent.getitematposition(position).tostring(); // selectedcity = spinner.getselecteditem().tostring(); if (position != 0) { //put actions here string ch =spinner_dept.get(position); toast.maketext(parent.getcontext(), "selected: " + selectedcity, toast.length_long).show(); } else { // nothing here or toast toast.maketext(parent.getcontext(), "selected: " , toast.length_long).show(); } // showing selected spinner item } public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } }
you not refreshing adapter after adding data. following changes , work.
// creating adapter spinner final arrayadapter<string> dataadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, spinner_dept); jsonobjectrequest jsonobjectrequest = new jsonobjectrequest(request.method.get, "http://192.168.1.104/leave/public/api/department" , null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { progressdialog.dismiss(); try { jsonarray jsonarray = response.getjsonarray("data"); (int = 0; < jsonarray.length(); i++) { jsonobject jsonobject = jsonarray.getjsonobject(i); string b = jsonarray.getjsonobject(i).getstring("department"); spinner_dept.add(b); json_id = jsonobject.getstring("identity"); json_dept = jsonobject.getstring("department"); department deprt = new department(); deprt.setid(json_id); deprt.setdepartment(json_dept); // spinner_data.add(deprt); // spinner_dept.add(jsonobject.getstring("department")); //textview.append( id+" "+ first_name + " " + last_name + "\n"); } //add refresh spinner adapter dataadapter.notifydatasetchanged(); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.d("error", "error server response"); } } ); requestqueue.add(jsonobjectrequest);
Comments
Post a Comment