views:

8756

answers:

7

How can you set the event listerner 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
}
+4  A: 
spinner1.setOnItemSelectedListener(
    new AdapterView.OnItemSelectedListener() {
        //add some code here
    }
);
Chiwai Chan
A: 
spinner1.setOnItemClickListener(
        new  AdapterView.OnItemSelectedListener() {           

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

   @Override
   public void onNothingSelected(AdapterView<?> parent) {

   }

        });
Tawani
This solution doesn't work for the Spinner widget. Quoting the Spinner documentation: "A spinner does not support item click events. Calling this method will raise an exception." -> use OnItemSelectedListener() instead, as described in my answer.
znq
A: 

I'm trying to do the same thing and the answer given by Tawani isn't correct - see docs here.

The trick that worked for me was inspired from Chiwai Chan's answer, so I believe his answer is the better one.

r1k0
+2  A: 

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

+19  A: 

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.

znq
A: 

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.

Your Eclipse environment is probably set up for Java 1.6. Android is only compatible with Java 1.5, where the `@Override` annotation is not allowed for interface methods.
Christopher
+1  A: 

Spinner spnLocale;

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 varable

Mine gets called twice too! I cant figure out how you differentiate between the two?
MAC