4

I'm new to Android and I'm trying to get a String from spinner. I've made some research but I couldn't find anything useful. Without trying to get String the code is working properly. This is the code which is working:

assert staticSpinner2 != null;
    staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();

        }

        public String getSpin(String spinnerLanguage2) {
            return spinnerLanguage2;
        }

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

        }
    });

I tried to use a getter method as follows where I didn't get any errors:

assert staticSpinner2 != null;
    staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();

        }

        public String getSpinnerString(String spinnerLanguage2) {
            return spinnerLanguage2;
        }

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

        }
    });

I'm trying to set this String to a text view using:

assert staticSpinner2 != null;
    staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();

        }

        public String getSpinnerString(String spinnerLanguage2) {
            return spinnerLanguage2;
        }

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

        }
    });

    MainActivityClassName AnObject = new MainActivityClassName();
    text.setText(AnObject.getSpinnerString());

Where I get an error saying "cannot resolve method getSpin()". I do realise I'm not passing any parameters to the method but I don't know how I can do it here. I appreciate any help or any other advice to solve the problem in a different way. Thanks in advance.

This is the full code if that helps:

public class MainActivity extends AppCompatActivity {

TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_json);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    text = (TextView) findViewById(R.id.textView);

    // spinner 1
    final Spinner staticSpinner1 = (Spinner) findViewById(R.id.spinner1);

    // Create an ArrayAdapter using the string array and a default spinner
    ArrayAdapter<CharSequence> staticAdapter1 = ArrayAdapter
            .createFromResource(this, R.array.lang,
                    android.R.layout.simple_spinner_item);

    // Specify the layout to use when the list of choices appears
    staticAdapter1
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // Apply the adapter to the spinner
    staticSpinner1.setAdapter(staticAdapter1);



    // spinner 2
    final Spinner staticSpinner2 = (Spinner) findViewById(R.id.spinner2);

    staticSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {
            String spinnerLanguage = staticSpinner1.getSelectedItem().toString();


            switch (spinnerLanguage) {
                case "Afrikaans":
                    // Create an ArrayAdapter using the string array and a default spinner
                    ArrayAdapter<CharSequence> staticAdapterAF = ArrayAdapter
                            .createFromResource(getBaseContext(), R.array.af,
                                    android.R.layout.simple_spinner_item);

                    // Specify the layout to use when the list of choices appears
                    staticAdapterAF
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    // Apply the adapter to the spinner
                    staticSpinner2.setAdapter(staticAdapterAF);
                    break;
                case "French":
                    // Create an ArrayAdapter using the string array and a default spinner
                    ArrayAdapter<CharSequence> staticAdapterFR = ArrayAdapter
                            .createFromResource(getBaseContext(), R.array.fr,
                                    android.R.layout.simple_spinner_item);

                    // Specify the layout to use when the list of choices appears
                    staticAdapterFR
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    // Apply the adapter to the spinner
                    staticSpinner2.setAdapter(staticAdapterFR);
                    break;
                case "English":
                    // Create an ArrayAdapter using the string array and a default spinner
                    ArrayAdapter<CharSequence> staticAdapterEN = ArrayAdapter
                            .createFromResource(getBaseContext(), R.array.en,
                                    android.R.layout.simple_spinner_item);

                    // Specify the layout to use when the list of choices appears
                    staticAdapterEN
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    // Apply the adapter to the spinner
                    staticSpinner2.setAdapter(staticAdapterEN);
                    break;
                case "Turkish":
                    // Create an ArrayAdapter using the string array and a default spinner
                    ArrayAdapter<CharSequence> staticAdapterTR = ArrayAdapter
                            .createFromResource(getBaseContext(), R.array.tr,
                                    android.R.layout.simple_spinner_item);

                    // Specify the layout to use when the list of choices appears
                    staticAdapterTR
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    // Apply the adapter to the spinner
                    staticSpinner2.setAdapter(staticAdapterTR);
                    break;
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }

    });


    assert staticSpinner2 != null;
    staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();


        }

        public String getSpin(String spinnerLanguage2) {
            return spinnerLanguage2;
        }

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

        }
    });

    MainActivity AnObject = new MainActivity();
    text.setText(AnObject.getSpinnerString());

}

}

Berat Cevik
  • 1,818
  • 3
  • 22
  • 28

3 Answers3

5

You can do this:

String selectedText = null;

staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        selectedText = parent.getItemAtPosition(position).toString();
    }

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

    }
});

Now you can use the selectedText variable to pass on to whatever method you want. But, keep in mind, unless you select something in the spinner, it's value would be null.

Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
  • When I did that Android Studio forced me to make selectedText a final String array. I tried it I don't get any errors know but the TextView has disappeared. – Berat Cevik Mar 21 '16 at 13:11
  • Can you post your complete code? Where's TextView coming from now? – Prerak Sola Mar 21 '16 at 13:14
  • What's `MainActivityClassName` ? – Prerak Sola Mar 21 '16 at 13:35
  • Sorry I meant MainActivity, still got the same error though. – Berat Cevik Mar 21 '16 at 14:54
  • Why do you need an object of `MainActivity` inside `MainActivity` ? I am not able to understand your workflow. What is your desired behavior when this activity gets launched. – Prerak Sola Mar 21 '16 at 15:08
  • I was trying to call the getSpin method through object reference. I am trying to set the value of the spinner's selected item to a TextView just to see if I can get strings from the second spinner which I can't. – Berat Cevik Mar 21 '16 at 16:54
  • I defined the variable outside the onCreate method, used your code and it worked. Thank you very much for your help. – Berat Cevik Mar 22 '16 at 17:55
4

If you really have to use it outer, you can do this :

 public void setTextView(String text){
        text.setText(text);
 }

and call this method inside your onItemSelected:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
     setTextView(staticSpinner2.getSelectedItem().toString());
}
Lucas Ferraz
  • 4,112
  • 2
  • 18
  • 23
  • This is working fine but what I actually need to do is to extract the string from onItemSelected method then I'm going to pass it to another main class method. I added setText here for simplicity. – Berat Cevik Mar 21 '16 at 12:59
  • Calling a external method like setTextView is a way to extract your String. Inside the setTextView you are on MainClass scope, and then, you can pass your String to any class you want. – Lucas Ferraz Mar 21 '16 at 13:09
2

Having read that piece of code, why don't you extract "spinnerLanguage2" to your main class and then access it from there? Assigning the value inside the OnItemSelected method.

EDIT: My previous answer is wrong. Try to implement an interface in order to communicate with the other class. Thus, calling a method of that interface you can pass the value. I give you a link with this (It explains it for fragments, but yo can communicate your classes too):

http://developer.android.com/intl/es/training/basics/fragments/communicating.html

kikettas
  • 1,682
  • 1
  • 24
  • 31
  • Can you please provide some code ti justify your answer. – Prerak Sola Mar 21 '16 at 12:52
  • Sorry, I have read it wrong, forget the previous answer. Maybe setting the text inside OnItemSelected Method would be the solution, thus you can control the textview whenever spinner changes. – kikettas Mar 21 '16 at 13:00
  • I know I can set the text inside the onItemSelected method. What I really need to do is to extract the string outside the method. I used setText here for simplicity. – Berat Cevik Mar 21 '16 at 13:02
  • I didn't know that you use it in a separated class, take a look to my edited comment. – kikettas Mar 21 '16 at 13:04