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);
It worked for me,
private boolean isSpinnerInitial = true;
If you are trying to avoid the initial call to your listener's
onItemSelected()
method, another option is to usepost()
to take advantage of the view's message queue. The first time the spinner checks for your listener it won't be set yet.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.Declare variable isSpinnerInitial then make Make a Selection as your default selection
spinnertaggeview.setSelection(-1); does not make selection as -1 or everything unselected as we do in .Net or other language . So you can ignore thatline.