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

拈花ヽ惹草 提交于 2019-12-20 03:26:44

问题


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 spinner selection & show a popup. The issue is when I select the 2nd item, the dialog appears but after closing the dialog and then again we select the same item, it won't show the dialog as onItemSelected will not be invoked. Is there any workaround for this? Without using a custom Spinner implementation.


回答1:


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



回答2:


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;
     }
[...]


来源:https://stackoverflow.com/questions/31742895/android-spinner-onitemselected-listener-is-not-called-when-selecting-the-same-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!