android - Spinner onItemSelected called indefinitely -
i have spinner , changing entries on every item click using setadapter. issue facing on click of spinner setonitemselectedlistener being called indefinite times. can see in below code confirmation box shown on click of every item , dialog box keeps appearing infinite number of times. below code
adapter_after_confirm = arrayadapter.createfromresource(this, r.array.spinner_after_confirmation, android.r.layout.simple_spinner_item); adapter_after_waiting = arrayadapter.createfromresource(this, r.array.spinner_after_waiting, android.r.layout.simple_spinner_item); adapter_after_boarded = arrayadapter.createfromresource(this, r.array.spinner_after_boarded, android.r.layout.simple_spinner_item); adapter_after_reached = arrayadapter.createfromresource(this, r.array.spinner_after_reached, android.r.layout.simple_spinner_item); // specify layout use when list of choices appears adapter_after_confirm.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); adapter_after_waiting.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); adapter_after_boarded.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); adapter_after_reached.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { string selected = parent.getitematposition(position).tostring(); if(spinnerselected){ if(selected.equals("waiting cab")){ confirmdialog("waiting cab"); spinnerstatuschange.setadapter(adapter_after_waiting); } else if(selected.equals("boarded cab")){ confirmdialog("boarded cab"); spinnerstatuschange.setadapter(adapter_after_boarded); } else if(selected.equals("reached destination")){ confirmdialog("boarded cab"); spinnerstatuschange.setadapter(adapter_after_reached); spinnerstatuschange.setenabled(false); toast.maketext(getapplicationcontext(), "location tracking disabled", toast.length_short).show(); } } } });
you not have use "if(spinnerselected)" because methot called when spinner selected.
try this:
adapter_after_confirm = arrayadapter.createfromresource(this, r.array.spinner_after_confirmation, android.r.layout.simple_spinner_item); adapter_after_waiting = arrayadapter.createfromresource(this, r.array.spinner_after_waiting, android.r.layout.simple_spinner_item); adapter_after_boarded = arrayadapter.createfromresource(this, r.array.spinner_after_boarded, android.r.layout.simple_spinner_item); adapter_after_reached = arrayadapter.createfromresource(this, r.array.spinner_after_reached, android.r.layout.simple_spinner_item); // specify layout use when list of choices appears adapter_after_confirm.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); adapter_after_waiting.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); adapter_after_boarded.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); adapter_after_reached.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { string selected = parent.getitematposition(position).tostring(); if(selected.equals("waiting cab")){ confirmdialog("waiting cab"); spinnerstatuschange.setadapter(adapter_after_waiting); } else if(selected.equals("boarded cab")){ confirmdialog("boarded cab"); spinnerstatuschange.setadapter(adapter_after_boarded); } else if(selected.equals("reached destination")){ confirmdialog("boarded cab"); spinnerstatuschange.setadapter(adapter_after_reached); spinnerstatuschange.setenabled(false); toast.maketext(getapplicationcontext(), "location tracking disabled", toast.length_short).show(); } } });
Comments
Post a Comment