0

My problem is that I can select a list item in a spinner drop down menu just fine, but I can't select an item multiple times in a row without first selecting another item. There must be a way to change a selection parameter in the parent class. Any idea how?

I have a few items in my spinner drop down menu.

dropdownMenu = (Spinner) findViewById(R.id.dropdownMenu);

List<String> list = new ArrayList<>();

list.add("Filters");
list.add("list 2");
list.add("list 3");

ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
        android.R.layout.simple_spinner_item, list);

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

dropdownMenu.setAdapter(dataAdapter);

My main activity extends the AdapterView.OnItemSelectedListener interface, and I set the listener using an instance of MainActivity

dropdownMenu.setOnItemSelectedListener(this);

Here are my interface overrides declared in MainActivity

public void onItemSelected(AdapterView<?> parent, View view,
                           int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
    Log.v(TAG,"SELECTED");

    // There must be a way to change the selected 
    // property to false here
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}
the_prole
  • 8,275
  • 16
  • 78
  • 163
  • Possible duplicate of [How can I get an event in Android Spinner when the current selected item is selected again?](https://stackoverflow.com/questions/5335306/how-can-i-get-an-event-in-android-spinner-when-the-current-selected-item-is-sele) – the_prole Jun 16 '17 at 22:21

2 Answers2

0

You need to implement Custom Spinner Like below:

public class CustomSpinner extends Spinner {

    public CustomSpinner(Context context) {
        super(context);
    }

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

    public CustomSpinner(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());
        }
    }
}
Patrick R
  • 6,621
  • 1
  • 24
  • 27
0

All Posted half Answer so posting complete implementation for ease to beginners and for better Understanding.

Create a Custom Spinner Class like Below.

import android.content.Context;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatSpinner;

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

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

Now In xml file, create a spinner. Replace the spinner tag with <yourdomain.yourprojectname.yourpackagename.spinnerclassname> for example <com.company.appname.utility.NDSpinner>.

In your java file, define the spinner variable as below: For example: -

NDSpinner ndSpinner = findViewById(R.id.spinnerId);

For Further Help Post Comment and get ans. ASAP