7

I've a Spinner with onItemSelected interation that works, but how the Api specification says:

This callback is invoked only when the newly selected position is different from the 
previously selected position or if there was no selected item.

I need to remove this limitation and i want that the callback is invoked also if the user select the same element. How to do that?
Anyone did the same thing?

Any idea about this would be appreciable..

Hulk
  • 2,565
  • 16
  • 24

3 Answers3

1

i want that the callback is invoked also if the user select the same element. How to do that?

Setting the OnItemClickListener for a Spinner will throw an exception and using ItemSelectedListener you will not be notified if the user click on the selected/same element.

I suppose the only way to overcome this limitation is to use a CustomAdapter for the Spinner items and implement the setOnClickListener for each view in the adapter.

Arun George
  • 18,352
  • 4
  • 28
  • 28
1

I had this same problem and looked around for a bit. There might be multiple ways of getting this functionality to work but extending the spinner worked for me. You could do something similar to what I found here.

So instead of using the default Android spinner extend it and add some code to it that will trigger your callback method.

I would like to add that using the setOnItemClickListener on a Spinner will throw an exception as stated in the documentation:

A spinner does not support item click events. Calling this method will raise an exception.
Community
  • 1
  • 1
Ali Derbane
  • 206
  • 2
  • 5
0

In this case you have to make a custom spinner: Try this

public class MySpinner extends Spinner{

OnItemSelectedListener listener;

public MySpinner(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

@Override
public void setSelection(int position)
{
    super.setSelection(position);

    if (position == getSelectedItemPosition())
    {
        listener.onItemSelected(null, null, position, 0);
    }       
}

public void setOnItemSelectedListener(OnItemSelectedListener listener)
{
    this.listener = listener;
}
}
Minkoo
  • 439
  • 1
  • 3
  • 16