Issue
I have created a spinner which is automatically updated with appliance names when a person adds an appliance using an array adapter. I created an OnItemSelected method with the spinner so when one of the names in the spinner is selected, a new window appears. However the OnItemSelected is automatically selecting the first item on the list when the activity starts and so the user does not have a chance to actually make a selection until the new window appears.
Here is the code:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
startActivity(new Intent("com.lukeorpin.theappliancekeeper.APPLIANCESELECTED"));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Does anyone know a way in which the first item on the list wont be automatically selected?
Here is the code for the rest of the spinner:
ArrayAdapter<String> appliancenameadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ApplianceNames); //Sets up an array adapter containing the values of the ApplianceNames string array
applianceName = (Spinner) findViewById(R.id.spinner_name); //Gives the spinner in the xml layout a variable name
applianceName.setAdapter(appliancenameadapter); //Adds the contents of the array adapter into the spinner
applianceName.setOnItemSelectedListener(this);
Solution
Does anyone know a way in which the first item on the list wont be automatically selected?
There is always a selection on Spinner
, and you cannot change that.
IMHO, you should not be using a Spinner
to trigger starting an activity.
That being said, you can use a boolean
to track whether this is the first selection event, and ignore it if it is.
Answered By - CommonsWare
Answer Checked By - David Goodson (JavaFixing Volunteer)