0

I am building an app and on my main activity I have a spinner, now I want the selected value to be send to my other activity when I press a button.

I have done this with succes for a couple of EditText's. But for a spinner it seems a bit tricky.

I am following the documentation from de developer.android.com site but I don't really understand it. link: http://developer.android.com/guide/topics/ui/controls/spinner.html

My code where I fill the spinner with data from an array: I do this code in the onCreate method:

//supply spinner with the array using an instance of ArrayAdapter
    Spinner spinner = (Spinner) findViewById(R.id.timespan_spinner);
    //create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.timespan_spinner, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

This is how I send the data drom the EditText's to my other activity: This is in my sendForm method

public void sendForm(View view) {
    //creating an Intent
    Intent intent = new Intent(this, DisplayFormActivity.class);
    //defining fields
    EditText editTextFirstname = (EditText) findViewById(R.id.txtFirstname);
    EditText editTextLastname = (EditText) findViewById(R.id.txtLastname);
    EditText editTextBedrag = (EditText) findViewById(R.id.txtBedrag);

    //getting the field values
    String firstname = editTextFirstname.getText().toString();
    String lastname = editTextLastname.getText().toString();
    String bedrag = editTextBedrag.getText().toString();

    //putting data in the intent
    intent.putExtra(FIRSTNAME, firstname);
    intent.putExtra(LASTNAME, lastname);
    intent.putExtra(BEDRAG, bedrag);

    startActivity(intent);
}
Moussa Chaabar
  • 501
  • 6
  • 18

1 Answers1

2

You can take a look at this answer, quoting:

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();

So, for your code:

public void sendForm(View view) {
        //creating an Intent
        Intent intent = new Intent(this, DisplayFormActivity.class);
        //defining fields
        EditText editTextFirstname = (EditText) findViewById(R.id.txtFirstname);
        EditText editTextLastname = (EditText) findViewById(R.id.txtLastname);
        EditText editTextBedrag = (EditText) findViewById(R.id.txtBedrag);
        Spinner spinner = (Spinner) findViewById(R.id.timespan_spinner);


        //getting the field values
        String firstname = editTextFirstname.getText().toString();
        String lastname = editTextLastname.getText().toString();
        String bedrag = editTextBedrag.getText().toString();
        String chosenOption = spinner.getSelectedItem().toString();

        //putting data in the intent
        intent.putExtra(FIRSTNAME, firstname);
        intent.putExtra(LASTNAME, lastname);
        intent.putExtra(BEDRAG, bedrag);
        intent.putExtra(CHOSEN_OPTION, chosenOption);

        startActivity(intent);
    }
Community
  • 1
  • 1
Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34