I've a Spinner with onItemSelected
interation that works, but how the Api specification says:
This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.
I need to remove this limitation and i want that the callback is invoked also if the user select the same element.
How to do that? I read a suggestion about extending Spinner class and set the position to INVALID_POSITION
, but i've not understood/able to do that.
Anyone did the same thing?
getSelectedItem(), Toast.
Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.
Android spinner is associated with AdapterView.
I also needed a solution to this problem. What I wanted to do was have a Spinner that has date ranges with a custom range option. The rows would look something like this:
Apr 10 - May 10
Mar 10 - Apr 10
Feb 10 - Mar 10
Custom Range
The problem is that if the user selects a custom range and then wants to change their custom range, they have to select a different range and then select the custom range option again. I wanted the user to just be able to select "Custom Range" again so that the custom range dialog could be shown again.
I sub-classed Spinner and created my own listener. The code switches the selection, but then immediately switches it so that nothing is selected. In my listener I just ignore any position that is less than zero.
The Spinner just displays the last selected item. I created my own custom adapter and specify what to display for each view, but that shouldn't be necessary. Here is how I sub-classed Spinner.
package com.example.widget;
import android.content.Context;
import android.widget.Spinner;
public class DateRangeSpinner extends Spinner {
private ItemSelectionListener listener;
public DateRangeSpinner(Context context) {
super(context);
}
/**
* This listener will be fired every time an item is selected,
* regardless of whether it has already been selected or not.
*
* @param l
*/
public void setOnItemSelectedListener(ItemSelectionListener l) {
listener = l;
}
public void removeOnItemSelectedListener() {
listener = null;
}
@Override
public void setSelection(int position) {
setSelection(position, true);
}
@Override
public void setSelection(int position, boolean animate) {
if (listener != null) {
listener.onItemSelected(position);
}
super.setSelection(position, animate);
super.setSelection(-1, animate);
}
public interface ItemSelectionListener {
public void onItemSelected(int position);
}
}
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With