Android Spinner OnItemSelected not called with the same item
Asked Answered
B

2

13

First of all, I know that this was asked several time, but on newer android versions it looks like that the suggested solutions doesn't work. I need that my spinner call OnItemSelected even when the user select the same item twice. I've found this class that should do the trick:

    public class NDSpinner extends Spinner {

    private int lastSelected = 0;
    private static Method s_pSelectionChangedMethod = null;


    static {        
        try {
            Class noparams[] = {};
            Class targetClass = AdapterView.class;

            s_pSelectionChangedMethod = targetClass.getDeclaredMethod("selectionChanged", noparams);            
            if (s_pSelectionChangedMethod != null) {
                s_pSelectionChangedMethod.setAccessible(true);              
            }

        } catch( Exception e ) {
            Log.e("Custom spinner, reflection bug:", e.getMessage());
            throw new RuntimeException(e);
        }
    }

    public NDSpinner(Context context) {
        super(context);
    }

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

    public NDSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }







@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if(this.lastSelected == this.getSelectedItemPosition())
        testReflectionForSelectionChanged();
    if(!changed)
        lastSelected = this.getSelectedItemPosition();

    super.onLayout(changed, l, t, r, b);
} 



    public void testReflectionForSelectionChanged() {
        try {
            Class noparams[] = {};          
            s_pSelectionChangedMethod.invoke(this, noparams);
        } catch (Exception e) {
            Log.e("Custom spinner, reflection bug: ", e.getMessage());
            e.printStackTrace();                
        }
    } 




    @Override
   public void onClick(DialogInterface dialog, int which) {    
        super.onClick(dialog, which);
    }
}

This infact works, but it has a bug: it call twice the item the first time :( Can anybody tell me how can I solve this ?

Thanks mates.

Boatel answered 1/11, 2013 at 19:33 Comment(1)
B
30

I've solved using this class:

public class NDSpinner extends Spinner {

      public NDSpinner(Context context)
      { super(context); }

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

      public NDSpinner(Context context, AttributeSet attrs, int defStyle)
      { super(context, attrs, defStyle); }

      @Override public void
      setSelection(int position, boolean animate)
      {
        boolean sameSelected = position == getSelectedItemPosition();
        super.setSelection(position, animate);
        if (sameSelected) {
          // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
          getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
        }
      }

      @Override public void
      setSelection(int position)
      {
        boolean sameSelected = position == getSelectedItemPosition();
        super.setSelection(position);
        if (sameSelected) {
          // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
          getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
        }
      }
    }

Thanks anyway :)

Boatel answered 1/11, 2013 at 23:13 Comment(4)
After several hours doing research finally found your answer. You saved my time a lot. Thanks man.Alexio
how can I use this in my activity?Fletafletch
@HammadNasir it is just a custom Spinner. Just use the NDSpinner object instead of the classic Spinner.Averi
boolean sameSelected = position == getSelectedItemPosition(); Always TRUE, getSelectedItemPosition() method return the same value as position, check my post - https://mcmap.net/q/504757/-spinner-onitemselected-not-called-when-selected-item-remains-the-sameIiette
H
0

For me, I extended AppCompatSpinner.

Also if your Spinneris in XML for layout, remember to change your

<Spinner...

to

<com.example.util.NDSpinner...
Hazzard answered 24/2, 2017 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.