3

I have been going through the threads regarding the spinners and when the onItemSelected is triggered. I concluded that it is triggered whether you manually select a spinner item or your programmatically select an item through the spinner.setSelection(position) method.

My problem is that OnItemSelected callback method is called first within my code, but it is executed after several other methods finish executing. This is an undesired behavior because my code logic depends on certain steps being executed in sequence.

if(conition is true)
  fillSpecialtySpinner();

if(another condition is true)
  fillSubSpecialtySpinner();

The fillSpecialtySpinner() method contains the spinner.setSelection(position) line of code. But somehow it is executed after the second if is checked and the fillSubSpecialtySpinner finishes executing.

Any help would be appreciated. Thank you in advance!

Nouran H
  • 1,992
  • 5
  • 18
  • 24

2 Answers2

0

If I understand correctly, you are wanting to make sure that fillSubSpecialtySpinner() method is only called after specialitySpinner.onItemSelected().

Can you just call fillSubSpecialtySpinner() from within specialitySpinner.onItemSelected()?

vaughandroid
  • 4,315
  • 1
  • 27
  • 33
  • I ended up moving my code to the specialtySpinner.onItemSelected(). Thank you, however I am still wondering about the behavior, in case someone out there knows why the onItemSelected event was triggered later in the code. – Nouran H May 29 '12 at 15:07
  • All Android UI events get added to an event queue, and the callbacks don't happen until control gets returned to the system. A lot of UI APIs work this way, it's mainly done to simplify threading issues. See here for more info: http://developer.android.com/resources/articles/painless-threading.html – vaughandroid May 29 '12 at 19:16
0

The callback doesn't happen until every other function is executed, yes, ad Baqueta said.

So you can't do something like set a boolean to false, make the change to the spinner, (execute code in onItemSelected if boolean is true), then set it to true, because the function will get called AFTER your boolean becomes true.

However, I have an easy and generic solution to the problem (Refer the accepted answer to the question):

Undesired onItemSelected calls

Community
  • 1
  • 1
Vedavyas Bhat
  • 2,068
  • 1
  • 21
  • 31