How can you set the event listener for a Spinner when the selected item changes?
Basically what I am trying to do is something similar to this:
spinn
This will work intialize the spinner and findviewbyid and use this it will work
Spinner schemeStatusSpinner;
schemeStatusSpinner = (Spinner) dialog.findViewById(R.id.spinner);
schemeStatusSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
if(schemeStatusSpinner.getSelectedItemId()==4){
reasonll.setVisibility(View.VISIBLE);
}
else {
reasonll.setVisibility(View.GONE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
If you want a true onChangedListener(). Store the initial value in the handler and check to see if it has changed. It is simple and does not require a global variable. Works if you have more than one spinner on the page.
String initialValue = // get from Database or your object
mySpinner.setOnItemSelectedListener(new SpinnerSelectedListener(initialValue));
...
protected class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener {
private SpinnerSelectedListener() {
super();
}
public SpinnerSelectedListener(String initialValue) {
this();
this.initialValue = initialValue;
}
private String initialValue;
// getter and setter removed.
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final String newValue = (String) spinHeight.getItemAtPosition(position);
if (newValue.equals(initialValue) == false) {
// Add your code here. The spinner has changed value.
// Maybe useful.
// initialValue = newValue;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Maybe useful.
// initialValue = null;
}
}
Objects are your friend, use them.
https://stackoverflow.com/q/1714426/811625
You can avoid the OnItemSelectedListener() being called with a simple check: Store the current selection index in an integer variable and check within the onItemSelected(..) before doing anything.
E.g:
Spinner spnLocale;
spnLocale = (Spinner)findViewById(R.id.spnLocale);
int iCurrentSelection = spnLocale.getSelectedItemPosition();
spnLocale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (iCurrentSelection != i){
// Your code here
}
iCurrentSelection = i;
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
Of cause the iCurrentSelection
should be in object scope for this to work!
By default, you will get the first item of the spinner array through
value = spinner.getSelectedItem().toString();
whenever you selected the value in the spinner this will give you the selected value
if you want the position of the selected item then do it like that
pos = spinner.getSelectedItemPosition();
the above two answers are for without applying listener