1. Are you ready for the Galaxy S20? Here is everything we know so far!

Spinner's currently selected item is repeatedly selected

Discussion in 'Android Development' started by Mingi, Jun 11, 2019.

  1. Mingi

    Mingi Lurker
    Thread Starter

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

    }
    });
    }
    ```
     


Loading...
Similar Threads - Spinner's currently selected
  1. GabeTheGreat73
    Replies:
    4
    Views:
    470
  2. towertech
    Replies:
    0
    Views:
    502
  3. ShamusVW
    Replies:
    3
    Views:
    771
  4. Prashant Sontale
    Replies:
    0
    Views:
    307
  5. Prashant Sontale
    Replies:
    0
    Views:
    270
  6. Prashant Sontale
    Replies:
    2
    Views:
    478
  7. Mingi
    Replies:
    0
    Views:
    297
  8. AbdouMike
    Replies:
    2
    Views:
    2,268
  9. Mingi
    Replies:
    1
    Views:
    1,809
  10. Mingi
    Replies:
    0
    Views:
    867

Share This Page

Loading...