使用Spinner中的setOnItemSelectedListener选择的相同位置

时间:2019-01-09 15:31:23

标签: android android-studio

当ArrayList的大小为0时,我正在使用setOnItemSelectedListener。 当您按相同的选项时,它不会返回Toast, 只需在更改选项时将其返回, 我想setOnItemSelectedListener做吐司 您按相同的选项。

我尝试使用setOnItemSelectedListener,并且当ArrayList的大小为0时, 当您按相同的选项时,它不会返回吐司, 只需在更改选项时执行

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) {

        }
    });

我什至想在按下某个选项时就看到吐司 如果选项相同。显然,当ArrayList的大小为0时。

1 个答案:

答案 0 :(得分:0)

针对以下内容创建自己的微调器:

Android Spinner OnItemSelected with the same item

这是代码:

 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);
    }
}