I am trying to get a Spinner to work in Android. It displays fine and I can select any one of the options in the list. But how do I transfer that to a string?
I would have thought in the code below that ‘selected’ would hold the selected string, but I get an ‘Illegal modifier for the local class YourItemSelectedListener; only abstract or final is permitted’ error on the ‘YourItemSelectedListener’.
What am I doing wrong?
Many thanks for any help.
Spinner spinnerFPS = (Spinner) findViewById(R.id.sp_FPS);
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.framesps, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerFPS.setAdapter(adapter);
spinnerFPS.setOnItemSelectedListener(new YourItemSelectedListener());
public class YourItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
Since you are using an array resource for spinner create a resource handle
with local array declaration with
getResources().getStringArray(R.array.framesps);and then use that handle to access the selected item using position variable:
Heres a code edit: