Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I invoke an action on the same selection of spinner value

I select the value from spinner and when I select the same value again then no action is performed on the click.

like image 722
kirti kiran Avatar asked Apr 15 '13 10:04

kirti kiran


People also ask

How can I get an event in android spinner when the current selected item is selected again?

getSelectedItem(), Toast.

Which function is used to get the selected item from the spinner?

getItemAtPosition(i).

Which is the listener associated with spinner?

Android spinner is associated with AdapterView.


1 Answers

Use this custom Spinner class...

/** 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());
    }
  }
}
like image 185
user1924779 Avatar answered Sep 22 '22 01:09

user1924779