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
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);