Organizational Research By

Surprising Reserch Topic

android spinner get the selected item change event


android spinner get the selected item change event  using -'android,events,spinner'

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:

spinner1.onSelectionChange = handleSelectionChange;

void handleSelectionChange(Object sender){
    //handle event
}

    

asked Oct 11, 2015 by vijayshukla80
0 votes
10 views



Related Hot Questions

10 Answers

0 votes

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.

answered Oct 11, 2015 by sandeep bhadauria
0 votes
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 OnclickListener event will execute twice - 1. Spinner initialization 2. User selected manually

Try to differentiate those two by using flag variable

answered Oct 11, 2015 by ankitarajoria4
0 votes

The docs for the spinner-widget says: "A spinner does not support item click events." You should use "setOnItemSelectedListener" to handle your problem

answered Oct 11, 2015 by sanjaypal1983
0 votes

You can implement AdapterView.OnItemSelectedListener class in your Activity.

And then use the below line within onCreate()

Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);

Then override these two methods:

public void onItemSelected(AdapterView parent, View v, int position, long id) {
    selection.setText(items[position]);
}

public void onNothingSelected(AdapterView parent) {
    selection.setText("");
}
answered Oct 11, 2015 by yogeshplv
0 votes

q/1714426/811625

You can avoid the OnItemSelectedListener() being called with a simple check: Store the current selection index in an integer variable and check within the onItemSelected(..) before doing anything.

E.g:

Spinner spnLocale;

spnLocale = (Spinner)findViewById(R.id.spnLocale);

int iCurrentSelection = spnLocale.getSelectedItemPosition();

spnLocale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView adapterView, View view, int i, long l) { 
    if (iCurrentSelection != i){
            // Your code here
    }
    iCurrentSelection = i;
    } 

    public void onNothingSelected(AdapterView adapterView) {
        return;
    } 
}); 

Of cause the iCurrentSelection should be in object scope for this to work!

answered Oct 11, 2015 by ankitarajoria4
0 votes

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
    }
});
answered Oct 11, 2015 by patilkiran.101
0 votes
spinner.setOnItemSelectedListener(
            new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView arg0, View arg1,
                        int arg2, long arg3) {

                    // TODO Auto-generated method stub
                }

                @Override
                public void onNothingSelected(AdapterView arg0) {
                    // TODO Auto-generated method stub

                }
                //add some code here
            }
        );
answered Oct 11, 2015 by nikhilapatil
0 votes

Brilliant the above by Stefan Klumpp also helped me a lot. Thank you. I am a newbie and the "@Override"s in it caused an error about "must override a Super class method of the same name". Eclipse suggested I should remove the Overrides. When I did this, it worked. Please don't ask me how or why.

answered Oct 11, 2015 by ashish singh
0 votes

The best way wht i think would be to have an flagitemselected=0; in oncreate. And on item selected event increment that flag i.e flagitemselected++; and then check if(flagitemselected!=1){// do your work here}.This will help i guess

answered Oct 11, 2015 by dhananjayksharma
0 votes

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.

answered Oct 11, 2015 by yogeshplv

...