Tip:
Highlight text to annotate it
X
This video explores Spinners, it's the 5th in a 5 part series on Input Controls in Android
The objectives are listed here with a time line for when they appear in the video
So, we end this with the dropdown list showing us the event fired
when we selected something from the dropdown list (just giving us the name and that TextView),
but clicking a Button queries the dropdown list and adds some text in front of the name we selected.
Now, we demonstrated it both ways that we might want to interact with our Spinner.
And again, I'm using the terms "dropdown list" and Spinner synonymously throughout this demo.
OK, now, the last thing to keep in mind is that we have perhaps one undesired behavior in the way this is functioning.
Let me leave the Spinners Activity completely and come back to it.
Notice that it says Data here.
That can be a problem, because what happened is the onItemSelected() method
was called the moment we came into the Activity and did processing,
and that's usually not which you want.
And, unfortunately, the way that this is control is designed,
it fires the method automatically when you come into the Activity.
The reason for that is
we wire up the OnItemSelectedListener in the onCreate method,
and we setContentView in the onCreate method which were required to do,
but the actual user interface itself is not drawn on the screen until the Activity gains focus.
(More information about that at this link: http://developer.android.com/guide/topics/ ui/how-android-draws.html).
It talks about "How Android Draws Views."
And if we read here, it says, "When an Activity receives focus,
it will be requested to draw its layout."
At the moment it receives focus.
So what's happening here is that when the Activity gains focus and it draws the Spinner on the screen,
it sees the drawing of the Spinner on the screen as a change to the item selected, and it fires the event.
So the event fires the moment the Activity becomes visible on the screen,
and what if we don't want that behavior?
Well, that's a slightly advanced topic (for the most elegant way to cover this),
but I will address it now,
and will address this more when we actually get into threading.
So, the problem is that this is wired up before the Spinner is drawn on the screen,
and when it is drawn on the screen, it treats that as an itemSelected change event.
So what we can do is a little trickery here (so that we don't wire up the OnItemSelectedListener
until after the Activity actually becomes visible on the screen).
Well, how can we delay that?