Android spinner, OnItemSelectedListener is not called when selecting the same item?









up vote
0
down vote

favorite












For a particular reason I need my spinner's OnItemSelectedListener gets called even when user chooses the same item, but normally it doesn't. So I don't know how it is possible!
Any trick or libraries that can solve my problem?










share|improve this question





















  • you can use OnItemClickListener event instead OnItemSelectedListener
    – Farrokh
    Nov 10 at 6:05











  • Sounds like OnItemClickListener is not intended to be used with Android's Spinner, because when I do it, the app crashes. @Farrokh
    – SSP
    Nov 10 at 6:09











  • do u have more than a spinner ???
    – hamid keyhani
    Nov 10 at 6:16










  • Yeah, I've got other widgets too. But I don't think it's a matter of their presence. Look, there is no issue to get solved. It's a problem that needs a solution. @hamidkeyhani
    – SSP
    Nov 10 at 6:27










  • i'm so sorry for my wrong answer, but you can try my new answer posted below
    – Farrokh
    Nov 10 at 6:30














up vote
0
down vote

favorite












For a particular reason I need my spinner's OnItemSelectedListener gets called even when user chooses the same item, but normally it doesn't. So I don't know how it is possible!
Any trick or libraries that can solve my problem?










share|improve this question





















  • you can use OnItemClickListener event instead OnItemSelectedListener
    – Farrokh
    Nov 10 at 6:05











  • Sounds like OnItemClickListener is not intended to be used with Android's Spinner, because when I do it, the app crashes. @Farrokh
    – SSP
    Nov 10 at 6:09











  • do u have more than a spinner ???
    – hamid keyhani
    Nov 10 at 6:16










  • Yeah, I've got other widgets too. But I don't think it's a matter of their presence. Look, there is no issue to get solved. It's a problem that needs a solution. @hamidkeyhani
    – SSP
    Nov 10 at 6:27










  • i'm so sorry for my wrong answer, but you can try my new answer posted below
    – Farrokh
    Nov 10 at 6:30












up vote
0
down vote

favorite









up vote
0
down vote

favorite











For a particular reason I need my spinner's OnItemSelectedListener gets called even when user chooses the same item, but normally it doesn't. So I don't know how it is possible!
Any trick or libraries that can solve my problem?










share|improve this question













For a particular reason I need my spinner's OnItemSelectedListener gets called even when user chooses the same item, but normally it doesn't. So I don't know how it is possible!
Any trick or libraries that can solve my problem?







android spinner






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 6:03









SSP

518




518











  • you can use OnItemClickListener event instead OnItemSelectedListener
    – Farrokh
    Nov 10 at 6:05











  • Sounds like OnItemClickListener is not intended to be used with Android's Spinner, because when I do it, the app crashes. @Farrokh
    – SSP
    Nov 10 at 6:09











  • do u have more than a spinner ???
    – hamid keyhani
    Nov 10 at 6:16










  • Yeah, I've got other widgets too. But I don't think it's a matter of their presence. Look, there is no issue to get solved. It's a problem that needs a solution. @hamidkeyhani
    – SSP
    Nov 10 at 6:27










  • i'm so sorry for my wrong answer, but you can try my new answer posted below
    – Farrokh
    Nov 10 at 6:30
















  • you can use OnItemClickListener event instead OnItemSelectedListener
    – Farrokh
    Nov 10 at 6:05











  • Sounds like OnItemClickListener is not intended to be used with Android's Spinner, because when I do it, the app crashes. @Farrokh
    – SSP
    Nov 10 at 6:09











  • do u have more than a spinner ???
    – hamid keyhani
    Nov 10 at 6:16










  • Yeah, I've got other widgets too. But I don't think it's a matter of their presence. Look, there is no issue to get solved. It's a problem that needs a solution. @hamidkeyhani
    – SSP
    Nov 10 at 6:27










  • i'm so sorry for my wrong answer, but you can try my new answer posted below
    – Farrokh
    Nov 10 at 6:30















you can use OnItemClickListener event instead OnItemSelectedListener
– Farrokh
Nov 10 at 6:05





you can use OnItemClickListener event instead OnItemSelectedListener
– Farrokh
Nov 10 at 6:05













Sounds like OnItemClickListener is not intended to be used with Android's Spinner, because when I do it, the app crashes. @Farrokh
– SSP
Nov 10 at 6:09





Sounds like OnItemClickListener is not intended to be used with Android's Spinner, because when I do it, the app crashes. @Farrokh
– SSP
Nov 10 at 6:09













do u have more than a spinner ???
– hamid keyhani
Nov 10 at 6:16




do u have more than a spinner ???
– hamid keyhani
Nov 10 at 6:16












Yeah, I've got other widgets too. But I don't think it's a matter of their presence. Look, there is no issue to get solved. It's a problem that needs a solution. @hamidkeyhani
– SSP
Nov 10 at 6:27




Yeah, I've got other widgets too. But I don't think it's a matter of their presence. Look, there is no issue to get solved. It's a problem that needs a solution. @hamidkeyhani
– SSP
Nov 10 at 6:27












i'm so sorry for my wrong answer, but you can try my new answer posted below
– Farrokh
Nov 10 at 6:30




i'm so sorry for my wrong answer, but you can try my new answer posted below
– Farrokh
Nov 10 at 6:30












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










ok

you can use addOnLayoutChangeListener



yourspinner.addOnLayoutChangeListener(new View.OnLayoutChangeListener() 
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
Toast.makeText(context, "item is : " + yourspinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();

);





share|improve this answer




















  • Doesn't help honestly. But it gave me the idea to get the Spinner popup view somehow then popup.addOnLayoutChangeListener! But how to get Spinner popup? @Farrokh
    – SSP
    Nov 10 at 6:38










  • i tried this code and do what you need, every time spinner layout changes, a toast appear on screen, it include selecting new item or clicking same item, what do you need exactly?
    – Farrokh
    Nov 10 at 6:41










  • I'm sorry. You are right! It is exactly what I want :)
    – SSP
    Nov 10 at 6:45

















up vote
0
down vote













Make sure about the context. you can context as.



  • this


  • MainActivity.this


  • getApplicationContext()


If you are using the spinner in Fragment then you can use context as




getActivity()




String selectedItem;
String data="Australia","Delhi","Agra","Mumbai","America","Tanzania","Arizona";
spinner=(Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data);
spinner.setPrompt("Select State");
spinner.setAdapter(ad);
String val=spinner.getSelectedItem().toString();

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
String val=spinner.getSelectedItem().toString();

selectedItem=data[i];

Toast.makeText(MainActivity.this, " "+selectedItem, Toast.LENGTH_SHORT).show();



@Override
public void onNothingSelected(AdapterView<?> adapterView)


);





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236422%2fandroid-spinner-onitemselectedlistener-is-not-called-when-selecting-the-same-it%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    ok

    you can use addOnLayoutChangeListener



    yourspinner.addOnLayoutChangeListener(new View.OnLayoutChangeListener() 
    @Override
    public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
    Toast.makeText(context, "item is : " + yourspinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();

    );





    share|improve this answer




















    • Doesn't help honestly. But it gave me the idea to get the Spinner popup view somehow then popup.addOnLayoutChangeListener! But how to get Spinner popup? @Farrokh
      – SSP
      Nov 10 at 6:38










    • i tried this code and do what you need, every time spinner layout changes, a toast appear on screen, it include selecting new item or clicking same item, what do you need exactly?
      – Farrokh
      Nov 10 at 6:41










    • I'm sorry. You are right! It is exactly what I want :)
      – SSP
      Nov 10 at 6:45














    up vote
    0
    down vote



    accepted










    ok

    you can use addOnLayoutChangeListener



    yourspinner.addOnLayoutChangeListener(new View.OnLayoutChangeListener() 
    @Override
    public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
    Toast.makeText(context, "item is : " + yourspinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();

    );





    share|improve this answer




















    • Doesn't help honestly. But it gave me the idea to get the Spinner popup view somehow then popup.addOnLayoutChangeListener! But how to get Spinner popup? @Farrokh
      – SSP
      Nov 10 at 6:38










    • i tried this code and do what you need, every time spinner layout changes, a toast appear on screen, it include selecting new item or clicking same item, what do you need exactly?
      – Farrokh
      Nov 10 at 6:41










    • I'm sorry. You are right! It is exactly what I want :)
      – SSP
      Nov 10 at 6:45












    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    ok

    you can use addOnLayoutChangeListener



    yourspinner.addOnLayoutChangeListener(new View.OnLayoutChangeListener() 
    @Override
    public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
    Toast.makeText(context, "item is : " + yourspinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();

    );





    share|improve this answer












    ok

    you can use addOnLayoutChangeListener



    yourspinner.addOnLayoutChangeListener(new View.OnLayoutChangeListener() 
    @Override
    public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
    Toast.makeText(context, "item is : " + yourspinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();

    );






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 6:29









    Farrokh

    1,0731615




    1,0731615











    • Doesn't help honestly. But it gave me the idea to get the Spinner popup view somehow then popup.addOnLayoutChangeListener! But how to get Spinner popup? @Farrokh
      – SSP
      Nov 10 at 6:38










    • i tried this code and do what you need, every time spinner layout changes, a toast appear on screen, it include selecting new item or clicking same item, what do you need exactly?
      – Farrokh
      Nov 10 at 6:41










    • I'm sorry. You are right! It is exactly what I want :)
      – SSP
      Nov 10 at 6:45
















    • Doesn't help honestly. But it gave me the idea to get the Spinner popup view somehow then popup.addOnLayoutChangeListener! But how to get Spinner popup? @Farrokh
      – SSP
      Nov 10 at 6:38










    • i tried this code and do what you need, every time spinner layout changes, a toast appear on screen, it include selecting new item or clicking same item, what do you need exactly?
      – Farrokh
      Nov 10 at 6:41










    • I'm sorry. You are right! It is exactly what I want :)
      – SSP
      Nov 10 at 6:45















    Doesn't help honestly. But it gave me the idea to get the Spinner popup view somehow then popup.addOnLayoutChangeListener! But how to get Spinner popup? @Farrokh
    – SSP
    Nov 10 at 6:38




    Doesn't help honestly. But it gave me the idea to get the Spinner popup view somehow then popup.addOnLayoutChangeListener! But how to get Spinner popup? @Farrokh
    – SSP
    Nov 10 at 6:38












    i tried this code and do what you need, every time spinner layout changes, a toast appear on screen, it include selecting new item or clicking same item, what do you need exactly?
    – Farrokh
    Nov 10 at 6:41




    i tried this code and do what you need, every time spinner layout changes, a toast appear on screen, it include selecting new item or clicking same item, what do you need exactly?
    – Farrokh
    Nov 10 at 6:41












    I'm sorry. You are right! It is exactly what I want :)
    – SSP
    Nov 10 at 6:45




    I'm sorry. You are right! It is exactly what I want :)
    – SSP
    Nov 10 at 6:45












    up vote
    0
    down vote













    Make sure about the context. you can context as.



    • this


    • MainActivity.this


    • getApplicationContext()


    If you are using the spinner in Fragment then you can use context as




    getActivity()




    String selectedItem;
    String data="Australia","Delhi","Agra","Mumbai","America","Tanzania","Arizona";
    spinner=(Spinner) findViewById(R.id.spinner);
    ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data);
    spinner.setPrompt("Select State");
    spinner.setAdapter(ad);
    String val=spinner.getSelectedItem().toString();

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
    String val=spinner.getSelectedItem().toString();

    selectedItem=data[i];

    Toast.makeText(MainActivity.this, " "+selectedItem, Toast.LENGTH_SHORT).show();



    @Override
    public void onNothingSelected(AdapterView<?> adapterView)


    );





    share|improve this answer


























      up vote
      0
      down vote













      Make sure about the context. you can context as.



      • this


      • MainActivity.this


      • getApplicationContext()


      If you are using the spinner in Fragment then you can use context as




      getActivity()




      String selectedItem;
      String data="Australia","Delhi","Agra","Mumbai","America","Tanzania","Arizona";
      spinner=(Spinner) findViewById(R.id.spinner);
      ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data);
      spinner.setPrompt("Select State");
      spinner.setAdapter(ad);
      String val=spinner.getSelectedItem().toString();

      spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
      @Override
      public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
      String val=spinner.getSelectedItem().toString();

      selectedItem=data[i];

      Toast.makeText(MainActivity.this, " "+selectedItem, Toast.LENGTH_SHORT).show();



      @Override
      public void onNothingSelected(AdapterView<?> adapterView)


      );





      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        Make sure about the context. you can context as.



        • this


        • MainActivity.this


        • getApplicationContext()


        If you are using the spinner in Fragment then you can use context as




        getActivity()




        String selectedItem;
        String data="Australia","Delhi","Agra","Mumbai","America","Tanzania","Arizona";
        spinner=(Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data);
        spinner.setPrompt("Select State");
        spinner.setAdapter(ad);
        String val=spinner.getSelectedItem().toString();

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
        String val=spinner.getSelectedItem().toString();

        selectedItem=data[i];

        Toast.makeText(MainActivity.this, " "+selectedItem, Toast.LENGTH_SHORT).show();



        @Override
        public void onNothingSelected(AdapterView<?> adapterView)


        );





        share|improve this answer














        Make sure about the context. you can context as.



        • this


        • MainActivity.this


        • getApplicationContext()


        If you are using the spinner in Fragment then you can use context as




        getActivity()




        String selectedItem;
        String data="Australia","Delhi","Agra","Mumbai","America","Tanzania","Arizona";
        spinner=(Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data);
        spinner.setPrompt("Select State");
        spinner.setAdapter(ad);
        String val=spinner.getSelectedItem().toString();

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
        String val=spinner.getSelectedItem().toString();

        selectedItem=data[i];

        Toast.makeText(MainActivity.this, " "+selectedItem, Toast.LENGTH_SHORT).show();



        @Override
        public void onNothingSelected(AdapterView<?> adapterView)


        );






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 12:02









        Rahul Sharma

        2,0041415




        2,0041415










        answered Nov 10 at 6:54









        Black4Guy

        65




        65



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236422%2fandroid-spinner-onitemselectedlistener-is-not-called-when-selecting-the-same-it%23new-answer', 'question_page');

            );

            Post as a guest














































































            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線