Just wondering how you handle the following problem: a result is calculated depending on two spinners\' selected items. To handle the UI things, i.e. a user picks a new item in
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
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:
currentPos1
, currentPos2
)onItemSelectedListener()
call a method like refreshMyResult()
or whatever.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.
It is very easy you can call the Spinner.setSelection(int position, boolean animate)
method with false
so the listeners will not react on the change.
I created a library that help for all, that no need to call item onClick action in Spinner For example:
spinner.setSelection(withAction,position);
where withAction is a boolean flag, that used for call or not item action
Link on Github: https://github.com/scijoker/spinner2
My solution is very easy. First initialize a global boolean variable.
boolean shouldWork = true;
Then use below code in your onCreate() method.
Spinner spinner = findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
if (shouldWork) {
// Do your actions here
}
else
shouldWork = true;
}
public void onNothingSelected(AdapterView<?> parentView) {
}
});
Now you can use the setSelection method in everwhere without invoking the onItemSelected() method by below code.
shouldWork = false;
spinner.setSelection(0);
Spinner.setSelection(int position, boolean animate) does trigger the listener on 4.3