Android Spinner: onItemSelected listener is not called when selecting the same item
Asked Answered
G

2

3

I have a requirement to show an AlertDialog when selecting the 2nd item in Spinner. I know that using onItemSelected we can listen to the spinner selection & show a popup. The issue is when I select the 2nd item, the dialog appears but after closing the dialog and then again we select the same item, it won't show the dialog as onItemSelected will not be invoked. Is there any workaround for this? Without using a custom Spinner implementation.

Gleeson answered 31/7, 2015 at 9:36 Comment(7)
Post your code.. Questions like these without code will tend people to down vote your question.Immunochemistry
@HarishSridharan while that's true most of the time, this is exactly the type of question where code is not really needed. If you know the situation it's mostly trivial to reconstruct what the OP's code may look like. Having a Spinner with a listener that checks the position arguments and does something is not a hard thing. I think some poeple downvote questions they don't understand, for some reason.Unpack
@Unpack Thanks..you said what I was supposed to comment..this doesn't require any code to understand. A spinner, its listener & its done.Gleeson
@sJy try spinner.setSelection(Spinner.INVALID_POSITION); in the listener, so onItemSelected always triggers an onNothingSelected event too. This means that you'll lose tracking the selected item, but I have a feeling it's not a big deal for this situation. You can also try only calling when position == 1.Unpack
@Unpack it didn't work..onNothingSelected is not called in any case..Gleeson
@sJy what about the the dialog showing up on second select? That would be the key here.Unpack
@Unpack The answer by Anoop is workingGleeson
E
4

Create a custom spinner

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

public class CustomSpinner extends Spinner {
    OnItemSelectedListener listener;
    private AdapterView<?> lastParent;
    private View lastView;
    private long lastId;

    public CustomSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        initlistner();
    }

    @Override
    public void setSelection(int position) {
        if (position == getSelectedItemPosition() && listener != null) {
            listener.onItemSelected(lastParent, lastView, position, lastId);
        } else {
            super.setSelection(position);
        }

    }

    private void initlistner() {
        // TODO Auto-generated method stub
        super.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                lastParent = parent;
                lastView = view;
                lastId = id;
                if (listener != null) {
                    listener.onItemSelected(parent, view, position, id);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
                if (listener != null) {
                    listener.onNothingSelected(parent);
                }
            }
        });

    }

    public void setOnItemSelectedEvenIfUnchangedListener(
            OnItemSelectedListener listener) {
        this.listener = listener;
    }

}

Set Listener

private OnItemSelectedListener listener;

listener = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {}

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    };

Pass the listener object to custom listener

cusSpinner.setOnItemSelectedEvenIfUnchangedListener(listener);
Eal answered 31/7, 2015 at 10:15 Comment(5)
Thanks..actually I needed if this can be done without custom spinner..I knew a Custom Spinner can easily handle this..Gleeson
I am trying to add this feature to react-native but am not sure how to modify it to acheive this, may you please give some tips on the minimal changes I need to make to achieve this in the file here - github.com/facebook/react-native/blob/… - I'm a javascript guy, this is my first venture into java.Semiporcelain
FYR- ISSUE 15556Eal
How to instantiate cusSpinner ?Disturbance
This is working solution, thanks for saving my dayGraceless
M
-3

try the code below:

spinner.setOnItemSelectedListener(this);

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
{
     switch(arg2)
     {
          case 0:
          {
               Dialog dialog = new Dialog(getApplicationContext());
               .......
               dialog.show();
               spinner.setSelection(0);
          }
          break;
     }
[...]
Moya answered 21/12, 2016 at 7:27 Comment(1)
Please test this code yourself & try whether it works as expected. (This doesn't work)Gleeson

© 2022 - 2024 — McMap. All rights reserved.