Android Spinner: Get the selected item change event

前端 未结 16 2218
挽巷
挽巷 2020-11-22 16:47

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         


        
相关标签:
16条回答
  • 2020-11-22 17:41

    Find your spinner name and find id then implement this method.

    spinnername.setOnItemSelectedListener(new OnItemSelectedListener() {
    
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    });
    
    0 讨论(0)
  • 2020-11-22 17:43

    Some of the previous answers are not correct. They work for other widgets and views, but the documentation for the Spinner widget clearly states:

    A spinner does not support item click events. Calling this method will raise an exception.

    Better use OnItemSelectedListener() instead:

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    
    });
    

    This works for me.

    Note that onItemSelected method is also invoked when the view is being build, so you can consider putting it inside onCreate() method call.

    0 讨论(0)
  • 2020-11-22 17:43

    One trick I found was putting your setOnItemSelectedListeners in onWindowFocusChanged instead of onCreate. I haven't found any bad side-effects to doing it this way, yet. Basically, set up the listeners after the window gets drawn. I'm not sure how often onWindowFocusChanged runs, but it's easy enough to create yourself a lock variable if you are finding it running too often.

    I think Android might be using a message-based processing system, and if you put it all in onCreate, you may run into situations where the spinner gets populated after it gets drawn. So, your listener will fire off after you set the item location. This is an educated guess, of course, but feel free to correct me on this.

    0 讨论(0)
  • 2020-11-22 17:47
    Spinner spnLocale = (Spinner)findViewById(R.id.spnLocale);
    
    spnLocale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
            // Your code here
        } 
    
        public void onNothingSelected(AdapterView<?> adapterView) {
            return;
        } 
    }); 
    

    Note: Remember one thing.

    Spinner OnItemSelectedListener event will execute twice:

    1. Spinner initialization
    2. User selected manually

    Try to differentiate those two by using flag variable.

    0 讨论(0)
  • 2020-11-22 17:49

    It doesn't matter will you set OnItemSelectedListener in onCreate or onStart - it will still be called during of Activity creation or start (respectively).
    So we can set it in onCreate (and NOT in onStart!).
    Just add a flag to figure out first initialisation:

    private Spinner mSpinner;
    private boolean mSpinnerInitialized;
    

    then in onCreate (or onCreateView) just:

    mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    if (!mSpinnerInitialized) {
                        mSpinnerInitialized = true;
                        return;
                    }
    
                    // do stuff
                }
    
                public void onNothingSelected(AdapterView<?> adapterView) {
                    return;
                }
            });
    
    0 讨论(0)
  • 2020-11-22 17:49

    The docs for the spinner-widget says

    A spinner does not support item click events.

    You should use setOnItemSelectedListener to handle your problem.

    0 讨论(0)
提交回复
热议问题