Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner onItemSelected with item already selected

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?

like image 429
user842504 Avatar asked Nov 21 '11 13:11

user842504


People also ask

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

getSelectedItem(), Toast.

What are the two events of spinner control in Android?

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.

Which is the listener associated with spinner?

Android spinner is associated with AdapterView.


1 Answers

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!

like image 139
TALE Avatar answered Oct 17 '22 18:10

TALE