How to disable onItemSelectedListener to be invoked when setting selected item by code
A cleaner solution, in my opinion, to differentiate between programmatic and user-initiated changes is the following:
Create your listener for the spinner as both an OnTouchListener and OnItemSelectedListener
public class SpinnerInteractionListener implements AdapterView.OnItemSelectedListener, View.OnTouchListener {
boolean userSelect = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
userSelect = true;
return false;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (userSelect) {
// Your selection handling code here
userSelect = false;
}
}
}
Add the listener to the spinner registering for both event types
SpinnerInteractionListener listener = new SpinnerInteractionListener();
mSpinnerView.setOnTouchListener(listener);
mSpinnerView.setOnItemSelectedListener(listener);
This way, any unexpected calls to your handler method due to initialization or re-initialization will be ignored.
Okay, I got it working the way I want to now.
The thing to understand here (and I did not when I was writing that question...) is that everything in Android runs in one thread - the UI thread.
Meaning: even though you set Spinner's values here and there: they are only updated (visually) and their listeners are only called after all methods you're currently in (like onCreate
, onResume
or whatever) are finished.
This allows the following:
- keep the selected positions in field variables. (like
currentPos1
,currentPos2
) - the listeners
onItemSelectedListener()
call a method likerefreshMyResult()
or whatever. - when setting positions programmatically, set the spinners and call your own refresh method manually right after that.
The refreshMyResult()
method looks like this:
int newPos1 = mySpinner1.getSelectedItemPosition();
int newPos2 = mySpinner2.getSelectedItemPosition();
// only do something if update is not done yet
if (newPos1 != currentPos1 || newPos2 != currentPos2) {
currentPos1 = newPos1;
currentPos2 = newPos2;
// do whatever has to be done to update things!
}
Because the listeners will be called later - and by then, the remembered position in currentPos is already updated - nothing will happen and no unnecessary update of anything else will take place. When a user selects a new value in one of the spinners, well - the update will be performed accordingly!
That's it! :-)
Ahh - one more thing: the answer to my question is: No. The listeners cannot be disabled (easily) and will be called whenever a value is changed.
I have an easier, and I think, better solution. Since I had to refresh the spinners even after initialization, this is a more generic approach. Please refer the accepted answer:
Undesired onItemSelected calls