Search code examples
androidspinner

Spinner's currently selected item is repeatedly selected


I would like to have the event occur even when the Spinner's currently selected item is repeatedly selected. For example, if 0 is selected and 0 is selected again, I want to make the event fly again.

public class MySpinner extends Spinner{

OnItemSelectedListener listener;

public MySpinner(Context context, AttributeSet attrs)
{
super(context, attrs);
}

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

if (position == getSelectedItemPosition())
{
listener.onItemSelected(null, null, position, 0);
} 
}

public void setOnItemSelectedListener(OnItemSelectedListener listener)
{
this.listener = listener;
}
}

I used this code to define an extension class. How do you apply this to your existing Spinner?

Below is the current source.

public static TextView mTXT_DRS4DL, mTXT_DRS4D_NXT;
public static Spinner mSPI_DRS4DL, mSPI_DRS4D_NXT;
public static ArrayList<String> mLIST_DRS4DL, mLIST_DRS4D_NXT;
public static ArrayAdapter<String> mADAPTER_DRS4DL, mADAPTER_DRS4D_NXT;
private static boolean isFirstSelected = true;
private static String mCheckString = "";

{

    mSPI_DRS4DL = PlatMainActivity.instance.findViewById(R.id.spi_DRS4DL);
    mLIST_DRS4DL = new ArrayList( Arrays.asList());
    mADAPTER_DRS4DL = new ArrayAdapter<>(PlatMainActivity.instance, R.layout.support_simple_spinner_dropdown_item, mLIST_DRS4DL);
    mSPI_DRS4DL.setAdapter(mADAPTER_DRS4DL);

    mSPI_DRS4D_NXT = PlatMainActivity.instance.findViewById(R.id.spi_DRS4D_NXT);
    mLIST_DRS4D_NXT = new ArrayList( Arrays.asList());
    mADAPTER_DRS4D_NXT = new ArrayAdapter<>(PlatMainActivity.instance, R.layout.support_simple_spinner_dropdown_item, mLIST_DRS4D_NXT);
    mSPI_DRS4D_NXT.setAdapter(mADAPTER_DRS4D_NXT);
}
public static void Search(String mSearch)
{
    String mRemoveName;

    if(mSearch.contains("DRS4DL")) {
        mRemoveName = mSensorSearch.replaceAll(":DRS4DL", "");
        mADAPTER_DRS4DL.add(mRemoveSensorName);
        mADAPTER_DRS4DL.notifyDataSetChanged();
    }
    else if(mSearch.contains("DRS4D-NXT")) {
        mRemoveName = mSensorSearch.replaceAll(":DRS4D-NXT", "");
        mADAPTER_DRS4D_NXT.add(mRemoveSensorName);
        mADAPTER_DRS4D_NXT.notifyDataSetChanged();
    }
}
public static void SensorSpinnerEvent()
{
    mSPI_DRS4DL.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            //AbstractDataInterface.SendCmdMessage(mMessage, AbstractDataInterface.SEND_TO_DATA, PlatCmdMessage.MsgTouch.Req.SelRadar, 0);
        }

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

        }
    });

    mSPI_DRS4D_NXT.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            if(isFirstSelected)
            {
                isFirstSelected = false;
            } else {
                switch (position) {
                    case 0:
                        mCheckString = mLIST_DRS4D_NXT.get(0);
                        Log.e("LOG", "---------------------------------------------------->> DRS4D_NXT Check String : " + mCheckString);
                        break;

                    case 1:
                        mCheckString = mLIST_DRS4D_NXT.get(1);
                        Log.e("LOG", "---------------------------------------------------->> DRS4D_NXT Check String : " + mCheckString);
                       break;

                    case 2:
                        mCheckString = mLIST_DRS4D_NXT.get(2);
                        Log.e("LOG", "---------------------------------------------------->> DRS4D_NXT Check String : " + mCheckString);
                        break;
                }
            }
        }

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

        }
    });
}

Solution

  • Make widget of MySpinner in your xml. Then Replace

    public static Spinner mSPI_DRS4DL, mSPI_DRS4D_NXT;
    

    with

    public static MySpinner mSPI_DRS4DL, mSPI_DRS4D_NXT;