Android Spinner: onItemSelected listener is not called when selecting the same item

后端 未结 2 894
情深已故
情深已故 2021-01-22 14:16

I have a requirement to show an AlertDialog when selecting the 2nd item in Spinner. I know that using onItemSelected we can listen to the

相关标签:
2条回答
  • 2021-01-22 14:23

    try the code below:

    spinner.setOnItemSelectedListener(this);
    
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
    {
         switch(arg2)
         {
              case 0:
              {
                   Dialog dialog = new Dialog(getApplicationContext());
                   .......
                   dialog.show();
                   spinner.setSelection(0);
              }
              break;
         }
    [...]
    
    0 讨论(0)
  • 2021-01-22 14:27

    Create a custom spinner

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Spinner;
    
    public class CustomSpinner extends Spinner {
        OnItemSelectedListener listener;
        private AdapterView<?> lastParent;
        private View lastView;
        private long lastId;
    
        public CustomSpinner(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            initlistner();
        }
    
        @Override
        public void setSelection(int position) {
            if (position == getSelectedItemPosition() && listener != null) {
                listener.onItemSelected(lastParent, lastView, position, lastId);
            } else {
                super.setSelection(position);
            }
    
        }
    
        private void initlistner() {
            // TODO Auto-generated method stub
            super.setOnItemSelectedListener(new OnItemSelectedListener() {
    
                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub
                    lastParent = parent;
                    lastView = view;
                    lastId = id;
                    if (listener != null) {
                        listener.onItemSelected(parent, view, position, id);
                    }
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    // TODO Auto-generated method stub
                    if (listener != null) {
                        listener.onNothingSelected(parent);
                    }
                }
            });
    
        }
    
        public void setOnItemSelectedEvenIfUnchangedListener(
                OnItemSelectedListener listener) {
            this.listener = listener;
        }
    
    }
    

    Set Listener

    private OnItemSelectedListener listener;
    
    listener = new OnItemSelectedListener() {
    
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {}
    
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
    
            }
        };
    

    Pass the listener object to custom listener

    cusSpinner.setOnItemSelectedEvenIfUnchangedListener(listener);
    
    0 讨论(0)
提交回复
热议问题