I have code that runs OnItemSelectedListener
event of spinner. So when I am in the method:
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// I want to do something here if it's a user who changed the the selected item
}
...how can I know whether the item selection was done programmatically or by a user action through the UI?
I don't know that this can be distinguished from within the method. Indeed, it is a problem that a lot of people are facing, that onItemSelected
is fired when the spinner is initiated. It seems that currently, the only workaround is to use an external variable for this.
private Boolean isUserAction = false;
...
public void onItemSelected( ... ) {
if( isUserAction ) {
// code for user initiated selection
} else {
// code for programmatic selection
// also triggers on init (hence the default false)
}
// reset variable, so that it will always be true unless tampered with
isUserAction = true;
}
public void myButtonClick( ... ) {
isUserAction = false;
mySpinner.setSelectedItem ( ... );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With