android - How to populate a spinner based on the first two spinners using setOnItemSelectedListener -


i have 3 spinners, first populated string xml file, second should populated based on selection of first , third 1 based on selection of second.

i have search , tried several logic through weekend no success. after setting setonitemselectedlistener on second , third spinners, none of them populated.

please me. sample code provided below

public class reg_prop extends activity implements adapterview.onitemselectedlistener {  //create reference button btnsave; spinner spinprovince, spindistrict, spinlocal; string spinpro, spindist;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_reg_prop);      toast.maketext(this, "please select province, district , local municipality " +             "property located", toast.length_long).show();      //initialise reference     btnsave = (button)findviewbyid(r.id.btnsave);     spinprovince = (spinner)findviewbyid(r.id.spinprovince);     spindistrict=(spinner)findviewbyid(r.id.spindistrict);     spinlocal=(spinner)findviewbyid(r.id.spinlocal);      //set onclick listener     btnsave.setonclicklistener(new view.onclicklistener() {         //@overridde         public void onclick(view v) {             try {                 intent ro = new intent(getapplicationcontext(), mainactivity.class);                 startactivity(ro);                 //toast.maketext(getapplicationcontext(), "login button pressed", toast.length_long).show();              } catch (exception e) {                 toast.maketext(getapplicationcontext(), "login window not display",                         toast.length_long).show();                 e.printstacktrace();             }         }     });      //set onitemselected listener     spinprovince.setonitemselectedlistener(this);     spindistrict.setonitemselectedlistener(this); }  @override public void onitemselected(adapterview<?> parent, view view, int position, long id) {      switch (view.getid()){          case r.id.spinprovince:             spinpro = string.valueof(spinprovince.getselecteditem());             toast.maketext(this, spinpro, toast.length_short).show();              if(spinpro.contentequals("eastern cape")){                 list<string> list = new arraylist<string>();                  list.add("select district/metro *");                 list.add("buffalo city");                 list.add("nelson mandela bay");                 list.add("alfred nzo district");                 list.add("amathole district");                 list.add("chris hani district");                 list.add("joe gqabi district");                 list.add("or tambo district");                 list.add("cacadu district");                  final arrayadapter<string> districtadapter = new arrayadapter<>(this,                         android.r.layout.simple_spinner_item, list);                 districtadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);                 districtadapter.notifydatasetchanged();                 spindistrict.setadapter(districtadapter);             }        }         else{             toast.maketext(this, "please select province property located", toast.length_long).show();         }          break;      case r.id.spindistrict:         spindist = spindistrict.getselecteditem().tostring();          if(spindist != "" && spindist != "select district/metro *" || spindist != "select district *"){              switch (spindist){                 case "buffalo city":                     list<string> list = new arraylist<string>();                      list.add("select district/metro *");                     list.add("...");                     list.add("n....");                     list.add("local");                     list.add("amathole district");                     list.add("chris hani district");                     list.add("joe gqabi district");                     list.add("or tambo district");                     list.add("cacadu district");                      final arrayadapter<string> localadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, list);                     localadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);                     localadapter.notifydatasetchanged();                     spinlocal.setadapter(localadapter);                      break;             }         }         else{             toast.maketext(this, "please select district municipality", toast.length_long).show();         }         break;      }     }  @override  public void onnothingselected(adapterview<?> parent) {  toast.maketext(this, "please select province, district , local municipality property located", toast.length_long).show();     } 

thanks read , tried me. have solved problem.

all had place setonitemselectedlistener first 2 spinners within oncreate use functions populate second , third spinner based on selection. note: first spinner populate string xml file.

below sample code inside oncreate:

 //set onitemselected listener     spinprovince.setonitemselectedlistener(new adapterview.onitemselectedlistener() {          @override         public void onitemselected(adapterview<?> parent, view view, int position, long id) {             string province = (string)parent.getitematposition(position);              if(province.matches("eastern cape")){                 spinpro = 1;                 populatedist();             }             else if(province.matches("free state")){                 spinpro = 2;                 populatedist();             }         }          @override         public void onnothingselected(adapterview<?> parent) {             toast.maketext(reg_prop.this, "please select province property located", toast.length_long).show();          }     });      spindistrict.setonitemselectedlistener(new adapterview.onitemselectedlistener(){          @override         public void onitemselected(adapterview<?> parent, view view, int position, long id) {             string spindist = (string)parent.getitematposition(position);              if(spindist.matches("buffalo city") && spinpro == 1){                 dist = 1;                 populatelocal();             }             else if(spindist.matches("nelson mandela bay") && spinpro == 1){                 dist = 2;                 populatelocal();             }         }          @override         public void onnothingselected(adapterview<?> parent) {          }     }); 

functions populate spinners

void populatedist(){     //eatern cape     if(spinpro == 1){         string [] ec = {"select district/metro *", "buffalo city", "nelson mandela bay",                 "alfred nzo district", "amathole district", "chris hani district",                 "joe gqabi district", "or tambo district", "cacadu district"};          arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, ec);         spindistrict.setadapter(adapter);     }     //free state     else if(spinpro == 2){         string [] fs = {"select district/metro *", "mangaung metropolitan", "fezile dabi district",                 "lejweleputswa district", "thabo mofutsanyana district", "xhariep district"};          arrayadapter<string> adapter2 = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, fs);         spindistrict.setadapter(adapter2);     }  }  void populatelocal(){     //baffalo city     if(dist == 1){         string [] bc = {"buffalo city"};          arrayadapter<string> adapterl1 = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, bc);         spinlocal.setadapter(adapterl1);     }     //nelson mandela bay     else if(dist == 2){         string [] nmb = {"nelson mandela bay"};          arrayadapter<string> adapterl2 = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, nmb);         spinlocal.setadapter(adapterl2);     }  } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -