changing spinners value based on other spinners from mysql database (Android) -
can point me tutorials or guides on this. how update value of 1 spinner based on selection of previous spinner. data values coming mysql database. have searched around have not found satisfactory answer. pls help.
spinner = (spinner) findviewbyid(r.id.spinner); spinner.setonitemselectedlistener(spinnerlistener); spinner2 = (spinner) findviewbyid(r.id.spinnersportcentername); spinner2.setonitemselectedlistener(spinnerlistener);
from above have 2 spinners. right im getting data database. , code below. getting data. need update value of second spinner based on item selected in first spinner. how do that? pretty clueless right now.
private void getdata(){ //creating string request stringrequest stringrequest = new stringrequest(facilityconfig.data_url, new response.listener<string>() { @override public void onresponse(string response) { jsonobject j = null; try { //parsing fetched json string json object j = new jsonobject(response); //storing array of json string our json array result = j.getjsonarray(facilityconfig.json_array); getfacility(result); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); //creating request queue requestqueue requestqueue = volley.newrequestqueue(this); //adding request queue requestqueue.add(stringrequest); } private void getfacility(jsonarray j){ //traversing through items in json array for(int i=0;i<j.length();i++){ try { //getting json object jsonobject json = j.getjsonobject(i); facilities.add(json.getstring (facilityconfig.tag_facility)); } catch (jsonexception e) { e.printstacktrace(); } } //setting adapter show items in spinner spinner.setadapter(new arrayadapter<string>(fragmentone.this, android.r.layout.simple_spinner_dropdown_item, facilities)); } private void getdata2(){ //creating string request stringrequest stringrequest = new stringrequest(spnameconfig.data_url, new response.listener<string>() { @override public void onresponse(string response) { jsonobject j = null; try { //parsing fetched json string json object j = new jsonobject(response); //storing array of json string our json array result = j.getjsonarray(spnameconfig.json_array2); getspname(result); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); //creating request queue requestqueue requestqueue = volley.newrequestqueue(this); //adding request queue requestqueue.add(stringrequest); } private void getspname(jsonarray j){ //traversing through items in json array for(int i=0;i<j.length();i++){ try { //getting json object jsonobject json = j.getjsonobject(i); sportcenternames.add(json.getstring(spnameconfig.tag_sportcentername)); } catch (jsonexception e) { e.printstacktrace(); } } //setting adapter show items in spinner spinner2.setadapter(new arrayadapter<string>(fragmentone.this, android.r.layout.simple_spinner_dropdown_item, sportcenternames)); } public class myonitemselectedlistener implements adapterview.onitemselectedlistener { context mcontext; public myonitemselectedlistener(context context){ this.mcontext = context; } private string getprice(int pos){ string price=""; try { //getting object of given index jsonobject json = result.getjsonobject(pos); //fetching name object price = json.getstring(facilityconfig.tag_price); } catch (jsonexception e) { e.printstacktrace(); } //returning name return price; } public void onitemselected(adapterview<?> parent, view v, int pos, long row) { switch (parent.getid()) { case r.id.spinner: toast.maketext(parent.getcontext(), "onitemselectedlistener : " + parent.getitematposition(pos).tostring(), toast.length_short).show(); textviewprice.settext(getprice(pos)); break; case r.id.spinnersportcentername: toast.maketext(parent.getcontext(), "onitemselectedlistener : " + parent.getitematposition(pos).tostring(), toast.length_short).show(); break; } } @override public void onnothingselected(adapterview<?> parent) { textviewprice.settext(""); } }
firstspinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() public void onitemselected(adapterview<?> parent, view view, int position, long id) { arrayadapter<string> newadaptor = new arrayadapter<string>(this, android.r.layout .simple_spinner_dropdown_item, value); secondspinner.setadapter(adapter); } @override public void onnothingselected(adapterview<?> parent) { toast.maketext(getapplicationcontext(),"please seelct country",toast.length_short).show(); });
hope work.
Comments
Post a Comment