0

I'm using setOnItemSelectedListener when ArrayList size is 0. It doesn't return the Toast when u press the same option, just return it when change the option, I want setOnItemSelectedListener do the Toast when u press same option.

I tried with setOnItemSelectedListener and when ArrayList size is 0, it doesn't return the toast when u press the same option, just do it when change the option

sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int 
        position, long id) {

            if(position==1){
                if(turismo1.size()!=0){
                    code......
                }else{
                    Toast.makeText(getApplicationContext(), "No hay 
                    turismos",
                        Toast.LENGTH_LONG).show();
                        l.setAdapter(adaptador1);
                }
            }else if(position==2){
                code....
                }else{
                    Toast.makeText(getApplicationContext(), "No hay 
                    transportes",Toast.LENGTH_LONG).show();
                     l.setAdapter(adaptador2);
                }
            }
        }

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

        }
    });

I want to see the toast when press an option even if the option is the same. Obviously when the ArrayList size is 0.

Md. Zakir Hossain
  • 1,082
  • 11
  • 24
  • 1
    Possible duplicate of [How can I get an event in Android Spinner when the current selected item is selected again?](https://stackoverflow.com/questions/5335306/how-can-i-get-an-event-in-android-spinner-when-the-current-selected-item-is-sele) – Jake Lee Jan 09 '19 at 15:39

1 Answers1

0

Create your own spinner regarding to :

Android Spinner OnItemSelected with the same item

Here is the code :

 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);
    }
}
Ali Khoshraftar
  • 521
  • 1
  • 4
  • 16