Android Spinner's onItemSelected executed twice when going back to Fragment -
i created spinner fragment populates data retrieved http callout. when fragment first created, populate spinner selection choices, set setonitemselectedlistener , set initial selection in oncreateview().
statespinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { public void onitemselected(adapterview<?> parent, view view, int position, long id) { if (spinnerposition != position) { spinnerposition = position; textview stateselected = (textview) view; string stateselectedstr = stateselected.gettext().tostring(); linearlayout ballotsdisplay = (linearlayout) getactivity().findviewbyid(r.id.ballotsdisplay); ballotsdisplay.removeallviews(); map<string, string> calloutparams = new hashmap<string, string>(); calloutparams.put("state", stateselectedstr); // create , execute asynctask retrieve ballots new retrieveballots().execute(calloutparams); } } public void onnothingselected(adapterview<?> parent) { return; } }); // set default selection spinner int defaultstate = adapter.getposition(userstate); if (defaultstate == -1) { defaultstate = 0; } statespinner.setselection(defaultstate);
when fragment created, works well, spinner position set default , spinner item selected once shown in log below:
5009-5009/com.project.test d/test﹕ oncreateview called 5009-5009/com.project.test d/test﹕ statespinner.setselection 5009-5009/com.project.test d/test﹕ onactivitycreated called 5009-5009/com.project.test d/test﹕ onresume called 5009-5009/com.project.test d/test﹕ spinner item selected
the problem occurs when navigate away fragment fragment (i store fragment onto backstack). when click go original fragment, spinner seems have item selected twice:
5009-5009/com.project.test d/test﹕ oncreateview called 5009-5009/com.project.test d/test﹕ statespinner.setselection 5009-5009/com.project.test d/test﹕ onactivitycreated called 5009-5009/com.project.test d/test﹕ onresume called 5009-5009/com.project.test d/test﹕ spinner item selected 5009-5009/com.project.test d/test﹕ spinner item selected
so have 2 questions:
1) why spinner register 2 item selection occurrences when returning button.
2) there fix prevent 2 item selections occurring? right fragment being populated duplicate data since retrieves data twice.
** edit ** after changing statespinner.setselection(defaultstate, false)
, null pointer exception @ ballotsdisplay.removeallviews();
seems ballotsdisplay set null reason change
stacktrace:
05-15 07:25:48.303 6153-6153/com.poliseewriters.polisee e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception @ com.polisee.ballotmeasures.ballotmeasuresfragment$1.onitemselected(ballotmeasuresfragment.java:287) @ android.widget.adapterview.fireonselected(adapterview.java:882) @ android.widget.adapterview.selectionchanged(adapterview.java:865) @ android.widget.adapterview.checkselectionchanged(adapterview.java:1017) @ android.widget.spinner.layout(spinner.java:363) @ android.widget.absspinner.setselectionint(absspinner.java:292) @ android.widget.absspinner.setselection(absspinner.java:269) @ com.polisee.ballotmeasures.ballotmeasuresfragment.setstatespinner(ballotmeasuresfragment.java:314) @ com.polisee.ballotmeasures.ballotmeasuresfragment.oncreateview(ballotmeasuresfragment.java:201) @ android.support.v4.app.fragment.performcreateview(fragment.java:1786) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:953) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1136) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:739) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1499) @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:456) @ android.os.handler.handlecallback(handler.java:605) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4441) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) @ dalvik.system.nativestart.main(native method)
** edit: updated code prevent onitemselected executing twice, needed add null check ballotsdisplay **
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { if (savedinstancestate != null) { log.d("test", "bundle = " + savedinstancestate.tostring()); } log.d("test", "oncreateview called"); view view = (view) inflater.inflate(r.layout.fragment_ballot_measures, container, false); setstatespinner(view); return view; } private void setstatespinner(view view) { try { states = utilities.getallstatenames(); } catch (exception e) { log.e("error", "error retrieving names: " + e.getmessage()); } spinner statespinner = (spinner) view.findviewbyid(r.id.statespinner); final arrayadapter<string> adapter = new arrayadapter<string>(getactivity(), r.layout.ballotmeasures_state_spinner, states); adapter.setdropdownviewresource(r.layout.ballotmeasures_state_spinner_dropdown); statespinner.setadapter(adapter); statespinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { // callback method invoke when state has been selected public void onitemselected(adapterview<?> parent, view view, int position, long id) { log.d("test", "spinner item selected"); textview stateselected = (textview) view; string stateselectedstr = stateselected.gettext().tostring(); // remove displayed views in layout linearlayout ballotsdisplay = (linearlayout) getactivity().findviewbyid(r.id.ballotsdisplay); if (ballotsdisplay != null) { ballotsdisplay.removeallviews(); } map<string, string> calloutparams = new hashmap<string, string>(); calloutparams.put("state", stateselectedstr); // asynctask execute data retrieval new retrieveballots().execute(calloutparams); } public void onnothingselected(adapterview<?> parent) { return; } }); // set default selection spinner int defaultstate = adapter.getposition(userstate); if (defaultstate == -1) { defaultstate = 0; } log.d("test", "statespinner.setselection"); statespinner.setselection(defaultstate, false); }
use statespinner.setselection(defaultstate, false);
in place of statespinner.setselection(defaultstate);
Comments
Post a Comment