Friday, 11 September 2015

Android spinner issues




1. onItemSelected not called when selected item remains the same. which means if user selected the same item on spinner again, spinner doesn't call "onItemSelected", because spinner think user didn't change their selection.

http://stackoverflow.com/questions/10854329/spinner-onitemselected-not-called-when-selected-item-remains-the-same

My solution:

public class NoDefaultSpinner extends Spinner {

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

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

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


2. how to make spinner without default value.

You spinner will automatically select the first item in the list for you when the user open the page. "select" means spinner automatically call "onItemSelected" function with the first item on the list selected.


I have no perfect solution/

1. you can put a "--please select--" item in to spinner. (at 0 position) but the system will still select it for you by default. (and call onItemSelected)
2.customize the spinner which i don't know how to do.


















No comments:

Post a comment