Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make On Item Selected not automatically choose the first entry

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);
like image 231
Sketzii Avatar asked Apr 12 '12 22:04

Sketzii


3 Answers

If you are trying to avoid the initial call to your listener's onItemSelected() method, another option is to use post() to take advantage of the view's message queue. The first time the spinner checks for your listener it won't be set yet.

// Set initial selection
spinner.setSelection(position);

// Post to avoid initial invocation
spinner.post(new Runnable() {
  @Override public void run() {
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // Only called when the user changes the selection
      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {
      }
    });
  }
});
like image 188
James Wald Avatar answered Nov 03 '22 03:11

James Wald


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.

like image 31
CommonsWare Avatar answered Nov 03 '22 01:11

CommonsWare


It worked for me,

private boolean isSpinnerInitial = true;

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {

        if(isSpinnerInitial)
        {
            isSpinnerInitial = false;
        }
        else  {
            // do your work...
        }

    }
like image 8
karan Avatar answered Nov 03 '22 01:11

karan