【问题标题】:Same position selected using setOnItemSelectedListener in Spinner在 Spinner 中使用 setOnItemSelectedListener 选择的相同位置
【发布时间】:2019-01-09 15:31:22
【问题描述】:

当 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时。

【问题讨论】:

标签: android android-studio


【解决方案1】:

创建你自己的微调器:

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

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多