1

I have a spinner included on my form. Now I need to pass the selected value and include it on my form request. Populating the Spinner is already working, I just need to get the selected value. I am fairly new to Android, so I will greatly appreciate any help or tips you can offer.

Here's the Spinner value:

//this method will execute when we pick an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    //Setting the values to textviews for a selected item
    tvID.setText(getName(position));
}

This is my submit button function:

btnSignup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String firstname = etFname.getText().toString();
                final String lastname = etLname.getText().toString();
                final String email = etEmail.getText().toString();
                final String phone = etContact.getText().toString();
                final String username = etUsername.getText().toString();
                final String password = etPassword.getText().toString();
                final String department = spinner.getSelectedItem().toString();
//GET the value of spinner and include it here

I will need the spinner value so I can include it on my request:

RegisterRequest registerRequest = new RegisterRequest(firstname, lastname, email, phone, username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);

EDIT: I tried the solutions below but I'm getting this error: enter image description here

Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
April G
  • 111
  • 1
  • 13
  • 1
    http://stackoverflow.com/a/30598142/1391818 if you are mapping some strings to values – Koborl Jun 06 '16 at 04:40
  • error and your question are not at all related :)... post `responseListener` class and for `RegisterRequest` what last parameter (return type) needs. – Bharatesh Jun 06 '16 at 05:19
  • @skadoosh yes thank you I already fixed my Request class. My bad. – April G Jun 06 '16 at 06:14

5 Answers5

1

You can save the selected value in a global string and then pass it to your request.

Something like this

String sel_spinner;

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //Setting the values to textviews for a selected item
        tvID.setText(getName(position));

         sel_spinner=getName(position);
    }

Then use this string here

btnSignup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String firstname = etFname.getText().toString();
                final String lastname = etLname.getText().toString();
                final String email = etEmail.getText().toString();
                final String phone = etContact.getText().toString();
                final String username = etUsername.getText().toString();
                final String password = etPassword.getText().toString();
                final String department = sel_spinner;

Hope this helps :)

Aradhna
  • 973
  • 10
  • 20
  • Thanks. Other answers also work, it's just that I need to get the ID value, not the Department Name that is shown in the spinner. So I used this solution instead and converted the String to int. Thank you so much. – April G Jun 06 '16 at 06:16
0

Try this,

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

This may helps you

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

To get the spinner value Use the following code

String text = mySpinner.getSelectedItem().toString();
Abdul Momen Khan
  • 199
  • 2
  • 13
0

If you above code is working as you mentioned and this method is calling

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    //Setting the values to textviews for a selected item
    tvID.setText(getName(position));
}

Then you can also get value like this.

btnSignup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String firstname = etFname.getText().toString();
                final String lastname = etLname.getText().toString();
                final String email = etEmail.getText().toString();
                final String phone = etContact.getText().toString();
                final String username = etUsername.getText().toString();
                final String password = etPassword.getText().toString();
                final String department =  tvID.getText().toString();

          }            
        }
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

Question1 :- How to get selected value of spinner

Answer :- For getting selected item of spinner you must use getSelectedItem() method on your spinner item you can do these by :-

public  String getName(int position) 
{  
          spinnerreferece.setSelection(position);
            String selVal = (String) spinnerreference.getSelectedItem();
  return selVal
}

Question 2:- How to pass value of selected item : Answer 2:- I am assuming u want to pass value to other activity :-

public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {
    String selValue = (String) getName(position);
tvID.setText(selValue);   
//it se
 cho=Integer.parseInt(selValue);
    changescreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (cho)
            {
                case 1:
                    Intent i=new Intent(getApplicationContext(),Secondactivity.class);
                    Log.d("xml", String.valueOf(i));
i.putExtra("key",selValue);           
             startActivity(i);

                    break;

                    }
                    }

By doing these two thing you can achieve your desired task .I hope it will help you .

Shubham Sharma
  • 2,763
  • 5
  • 31
  • 46