I have a spinner in my android app which is used to call an api to change the data in list view bellow it. I have used OnItemSelectedListener for this, but when I click the same item again, nothing happens, unless I change it to some other item and again click on the item I needed. I want to use something similar to OnItemClick, as OnItemClick is not supported by spinner. Suggest me an alternative, please.
Asked
Active
Viewed 74 times
1 Answers
0
Took from this answer. Unfortunately, android Spinner doesn't provide functionality, that you want, but you can extend it and handle clicks manually:
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());
}
}
}
}

Community
- 1
- 1

Michael Spitsin
- 2,539
- 2
- 19
- 29