Calling value from OnItemSelectedListenerStop EditText from gaining focus at Activity startupfetching phone number according to nameAndroid Spinner get selected value OnSelectedItemListener();BadTokenException when click on spinner in an ActivityGroupError while rotating an image in Android EclipsegetFragmentManager returning null pointer exceptionAndroid Spinner with custom java object showing key value pairing instead of valueSpinner wont work with OnItemSelectedListenerHow to call radio buttons inside an onItemSelected method?How to fix ViewRootImpl: sendUserActionEvent() mView == null in spinner

Why did the EU agree to delay the Brexit deadline?

Why should universal income be universal?

If a character has darkvision, can they see through an area of nonmagical darkness filled with lightly obscuring gas?

Approximating irrational number to rational number

What is Cash Advance APR?

What are the purposes of autoencoders?

What is this called? Old film camera viewer?

Is there a working SACD iso player for Ubuntu?

Open a doc from terminal, but not by its name

Yosemite Fire Rings - What to Expect?

Pre-mixing cryogenic fuels and using only one fuel tank

Should I stop contributing to retirement accounts?

Start making guitar arrangements

Is a bound state a stationary state?

How do I color the graph in datavisualization?

Melting point of aspirin, contradicting sources

Aragorn's "guise" in the Orthanc Stone

Problem with TransformedDistribution

Is it improper etiquette to ask your opponent what his/her rating is before the game?

How to implement a feedback to keep the DC gain at zero for this conceptual passive filter?

Freedom of speech and where it applies

Argument list too long when zipping large list of certain files in a folder

Is the U.S. Code copyrighted by the Government?

When a Cleric spontaneously casts a Cure Light Wounds spell, will a Pearl of Power recover the original spell or Cure Light Wounds?



Calling value from OnItemSelectedListener


Stop EditText from gaining focus at Activity startupfetching phone number according to nameAndroid Spinner get selected value OnSelectedItemListener();BadTokenException when click on spinner in an ActivityGroupError while rotating an image in Android EclipsegetFragmentManager returning null pointer exceptionAndroid Spinner with custom java object showing key value pairing instead of valueSpinner wont work with OnItemSelectedListenerHow to call radio buttons inside an onItemSelected method?How to fix ViewRootImpl: sendUserActionEvent() mView == null in spinner













0















I have a value from the selected item in spinner passed to a String variable 'selectedSpinner' using spinner.setOnItemSelectedListener(). I am trying to access 'selectedSpinner' outside of the method.



I have tried to assign 'selectedSpinner' to another String 'selectedCategory' but outside of the spinner.setOnItemSelectedListener() method, 'selectedSpinner' could not be resolved. What should be done to access 'selectedSpinner' outside of the method? Thanks



private void recordDialog() 

...
//get category
final Spinner spinner = subView.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.category, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
String selectedSpinner = parent.getItemAtPosition(position).toString();


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


);
final String selectedCategory = selectedSpinner;
...
final AlertDialog alertDialog = builder.create();
alertDialog.show();










share|improve this question




























    0















    I have a value from the selected item in spinner passed to a String variable 'selectedSpinner' using spinner.setOnItemSelectedListener(). I am trying to access 'selectedSpinner' outside of the method.



    I have tried to assign 'selectedSpinner' to another String 'selectedCategory' but outside of the spinner.setOnItemSelectedListener() method, 'selectedSpinner' could not be resolved. What should be done to access 'selectedSpinner' outside of the method? Thanks



    private void recordDialog() 

    ...
    //get category
    final Spinner spinner = subView.findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.category, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    String selectedSpinner = parent.getItemAtPosition(position).toString();


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


    );
    final String selectedCategory = selectedSpinner;
    ...
    final AlertDialog alertDialog = builder.create();
    alertDialog.show();










    share|improve this question


























      0












      0








      0








      I have a value from the selected item in spinner passed to a String variable 'selectedSpinner' using spinner.setOnItemSelectedListener(). I am trying to access 'selectedSpinner' outside of the method.



      I have tried to assign 'selectedSpinner' to another String 'selectedCategory' but outside of the spinner.setOnItemSelectedListener() method, 'selectedSpinner' could not be resolved. What should be done to access 'selectedSpinner' outside of the method? Thanks



      private void recordDialog() 

      ...
      //get category
      final Spinner spinner = subView.findViewById(R.id.spinner);
      ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.category, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(adapter);
      spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
      String selectedSpinner = parent.getItemAtPosition(position).toString();


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


      );
      final String selectedCategory = selectedSpinner;
      ...
      final AlertDialog alertDialog = builder.create();
      alertDialog.show();










      share|improve this question
















      I have a value from the selected item in spinner passed to a String variable 'selectedSpinner' using spinner.setOnItemSelectedListener(). I am trying to access 'selectedSpinner' outside of the method.



      I have tried to assign 'selectedSpinner' to another String 'selectedCategory' but outside of the spinner.setOnItemSelectedListener() method, 'selectedSpinner' could not be resolved. What should be done to access 'selectedSpinner' outside of the method? Thanks



      private void recordDialog() 

      ...
      //get category
      final Spinner spinner = subView.findViewById(R.id.spinner);
      ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.category, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(adapter);
      spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
      String selectedSpinner = parent.getItemAtPosition(position).toString();


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


      );
      final String selectedCategory = selectedSpinner;
      ...
      final AlertDialog alertDialog = builder.create();
      alertDialog.show();







      android android-spinner onitemselectedlistener






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 7 at 11:07









      Tanveer Munir

      1,517422




      1,517422










      asked Mar 7 at 7:57









      Tan YiLiangTan YiLiang

      212




      212






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You need to declare selectedSpinner outside recordDialog function (or set it as global)



          String selectedSpinner;
          ...
          private void recordDialog()
          ......
          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
          selectedSpinner = parent.getItemAtPosition(position).toString();




          After that you can assign selectedSpinner to selectedCategory.






          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',
            autoActivateHeartbeat: false,
            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%2f55038735%2fcalling-value-from-onitemselectedlistener%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You need to declare selectedSpinner outside recordDialog function (or set it as global)



            String selectedSpinner;
            ...
            private void recordDialog()
            ......
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            selectedSpinner = parent.getItemAtPosition(position).toString();




            After that you can assign selectedSpinner to selectedCategory.






            share|improve this answer





























              0














              You need to declare selectedSpinner outside recordDialog function (or set it as global)



              String selectedSpinner;
              ...
              private void recordDialog()
              ......
              @Override
              public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
              selectedSpinner = parent.getItemAtPosition(position).toString();




              After that you can assign selectedSpinner to selectedCategory.






              share|improve this answer



























                0












                0








                0







                You need to declare selectedSpinner outside recordDialog function (or set it as global)



                String selectedSpinner;
                ...
                private void recordDialog()
                ......
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
                selectedSpinner = parent.getItemAtPosition(position).toString();




                After that you can assign selectedSpinner to selectedCategory.






                share|improve this answer















                You need to declare selectedSpinner outside recordDialog function (or set it as global)



                String selectedSpinner;
                ...
                private void recordDialog()
                ......
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
                selectedSpinner = parent.getItemAtPosition(position).toString();




                After that you can assign selectedSpinner to selectedCategory.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 7 at 8:07

























                answered Mar 7 at 8:00









                John JoeJohn Joe

                4,96032361




                4,96032361





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038735%2fcalling-value-from-onitemselectedlistener%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко