Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving selected item from spinner?

I am trying to retrieve the value spinner selected and I used the following code

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        Object item = parent.getItemAtPosition(pos);
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

But i cant use the value of item outside the function!I am very new to java please someone help me.. i tried to implement voice recognition in android

     public void speakButtonClicked(View v)
                   {
                       startVoiceRecognitionActivity();
                   }

                   /**
                    * Fire an intent to start the voice recognition activity.
                    */
                   private void startVoiceRecognitionActivity()
                   {
                       Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);


 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
                    startActivityForResult(intent, REQUEST_CODE);
               }

               /**
                * Handle the results from the voice recognition activity.
                */
               @Override
            protected
               void onActivityResult(int requestCode, int resultCode, Intent data) 
               {


                   if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
                   {
                       // Populate the spinner with the String values the recognition engine thought it heard

                       ArrayList<String> matches = data.getStringArrayListExtra(
                               RecognizerIntent.EXTRA_RESULTS);
                       final  Button button12=(Button)findViewById(R.id.button12);

                       final Spinner Speech_spinner=(Spinner)findViewById(R.id.spinner3);         


                    Speech_spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                              matches));
                    Speech_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                            Text = parent.getItemAtPosition(pos).toString();

                        }
                        public void onNothingSelected(AdapterView<?> parent) {
                        }
                    });

                        button12.setText(Text); 




                        }
                   super.onActivityResult(requestCode, resultCode, data);


               }

No value is coming in button12

like image 755
Soumya Avatar asked Mar 14 '12 11:03

Soumya


People also ask

Which function is used to get the selected item from the spinner?

getItemAtPosition(i).

How do I get the value from spinner in Kotlin?

Android Dependency Injection using Dagger with Kotlin This example demonstrates how to get Spinner value in Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

use final object like:

final Object item;

outside the onCreate

like image 50
thenosic Avatar answered Sep 21 '22 01:09

thenosic


declare Object item outside the oncreate so that it is available to the entire class.

like image 44
Anurag Ramdasan Avatar answered Sep 21 '22 01:09

Anurag Ramdasan