user1055656
user1055656

Reputation: 1

Android: Display Contents of Spinner Selection in EditText

I am working on an android project and came to a halt in my design process. I am a beginning java programmer and also new to the android sdk so please bear with me... On my main screen, it prompts the user to make 6 selections from separate spinner drop down menus. Each of the 6 spinners contain the same StringArray. What I want to do is display the 6 different spinner selections within an EditText field on another screen when a 'submit' button is clicked. I have the submit button listener set up correctly along with a new activity and intent to switch the layout to the output screen. What I don't understand is how to take the spinner(s) and display them into the text fields. I have tried setting up 6 individual SetOnItemSelectedListener methods but unsure if that is allowed. Help, please and thank you!

Upvotes: 0

Views: 1344

Answers (1)

Dan Calinescu
Dan Calinescu

Reputation: 71

I sugggest you setup your spinners with a simple ArrayAdapter like so:

  String[] selections = new String[] { "Selection 1", "Selection 2", "Selection 3", "Selection 4" };

  ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(mySpinner1.getContext(), android.R.layout.simple_spinner_item, selections);
  myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  mySpinner1.setAdapter(myAdapter);

Follow the same concept for all 6 spinners. And then when you fetch their values like so:

   String value1 = mySpinner1.getSelectedItem().toString();
   String value2 = mySpinner2.getSelectedItem().toString();       
   String value3 = mySpinner2.getSelectedItem().toString();
   String value4 = mySpinner2.getSelectedItem().toString();
   String value5 = mySpinner2.getSelectedItem().toString();
   String value6 = mySpinner2.getSelectedItem().toString();

Now you can concatenate these string as needed and display them in your text view like so:

   myTextView.setText(value1 + "," + value2 + "," + value3 + "," + value4 + "," + value5 + "," + value6);

Hope that helps. Have fun.

Upvotes: 2

Related Questions