android spinner fire event when same item selectio

2020-02-05 13:30发布

I want to fire a event when the same item is selected in spinner. Method

@Override
    public void onItemSelected(AdapterView<?> parent, View arg1, int position,
            long arg3) {
    }

is called only when we different selection is made. My Purpose is to display a toast when any item is selected either the same item is reselected or different selection is made.

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

    }

above method does not solve my problem.

5条回答
老娘就宠你
2楼-- · 2020-02-05 13:41

Since my reputation is not high enough to comment directly on @Suat 's answer, I tried that method, it works like charm, but I'm not clear what the side effects could be. Something I want to add is, additional constructors should be added to avoid errors.

public SpinnerTrigger(Context context, AttributeSet attrs, int defStyle)
{ super(context, attrs, defStyle); }

public SpinnerTrigger(Context context, AttributeSet attrs){
super(context,attrs);

}

查看更多
孤傲高冷的网名
3楼-- · 2020-02-05 13:43

use click listener to fulfill your requirement. as direct click listener on spinner doesn't supported so make a class extend spinner and over ride on click method and in this method do what you want to do.

查看更多
Ridiculous、
4楼-- · 2020-02-05 13:47

You can add a method name on your item selected METHOD

Spinner `Spinner1`=(Spinner)findViewById(R.id.`declareid`)

oBject has been declared for spinner

 @Override
        public void onItemSelected(AdapterView<?> parent, View arg1, int position,
                long arg3) 
    {
    ItemOnChange();
       }

private void ItemOnChange() {

        if(Spinner1.getSelectedItemPosition()>0){
        pd=ProgressDialog.show(this,"","Loading, Please wait .. ",true);

            final int spinner=Spinner1.getSelectedItemPosition();


            final Handler ThreadCallback=new Handler();
            final Runnable runInCityThread=new Runnable(){
                public void run(){
                    fnBindspimmer2();
                    pd.dismiss();
                }

            };

            new Thread(){
                @Override public void run(){

                Spinner2values();
                ThreadCallback.post(runInCityThread);
                }

            }.start();
        }



}
查看更多
相关推荐>>
5楼-- · 2020-02-05 14:01

Hopefully this help. I tried and it works

/** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */
    public class NDSpinner extends Spinner {

      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 public void
      setSelection(int position, boolean animate)
      {
        boolean sameSelected = position == getSelectedItemPosition();
        super.setSelection(position, animate);
        if (sameSelected) {
          // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
          getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
        }
      }

      @Override public void
      setSelection(int position)
      {
        boolean sameSelected = position == getSelectedItemPosition();
        super.setSelection(position);
        if (sameSelected) {
          // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
          getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
        }
      }
    }
查看更多
We Are One
6楼-- · 2020-02-05 14:07

i have found that old selection is kept at variable called mOldSelectedPosition in the hierarcy of the spinner. Spinner is using this value to check if the same item selected or not , and if it is the same , it ignores. If we dont wanna ignore this What i did is some dirty code using reflection.

package com.aradiom.amc.nativecomponents;

import java.lang.reflect.Field;

import android.content.Context;
import android.util.Log;
import android.widget.Spinner;

public class SpinnerTrigger extends Spinner {

public SpinnerTrigger(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
public void setSelection(int position, boolean animate) {
    ignoreOldSelectionByReflection();
    super.setSelection(position, animate);
}

private void ignoreOldSelectionByReflection() {
    try {
        Class<?> c = this.getClass().getSuperclass().getSuperclass().getSuperclass();
        Field reqField = c.getDeclaredField("mOldSelectedPosition");
        reqField.setAccessible(true);
        reqField.setInt(this, -1);
    } catch (Exception e) {
        Log.d("Exception Private", "ex", e);
        // TODO: handle exception
    }
}

@Override
public void setSelection(int position) {
    ignoreOldSelectionByReflection();
    super.setSelection(position);
}

}

This class will always invalidate the oldselection value , so that every time on click event gets triggered. It may not be perfect solution. Use with caution. :)

查看更多
登录 后发表回答