How to keep onItemSelected from firing off on a newly instantiated Spinner?
Referring to the answer of Dan Dyer, try to register the OnSelectListener
in a post(Runnable)
method:
spinner.post(new Runnable() {
public void run() {
spinner.setOnItemSelectedListener(listener);
}
});
By doing that for me the wished behavior finally occurred.
In this case it also means that the listener only fires on a changed item.
The use of Runnables is completely incorrect.
Use setSelection(position, false);
in the initial selection before setOnItemSelectedListener(listener)
This way you set your selection with no animation which causes the on item selected listener to be called. But the listener is null so nothing is run. Then your listener is assigned.
So follow this exact sequence:
Spinner s = (Spinner)Util.findViewById(view, R.id.sound, R.id.spinner);
s.setAdapter(adapter);
s.setSelection(position, false);
s.setOnItemSelectedListener(listener);
I would have expected your solution to work -- I though the selection event would not fire if you set the adapter before setting up the listener.
That being said, a simple boolean flag would allow you to detect the rogue first selection event and ignore it.