How to return the selected item from a spinner

Asked

Viewed 284 times

-1

How to pass to a variable the value selected in a spineer at the moment the user selects some of the options?

I tried to implement this option, but I was unsuccessful.

Follow my code in case anyone can tell me where the bug is:

public class desafio extends AppCompatActivity {

    EditText campo1, campo2;
    int pos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_desafio);

        String[] operacoes = {"+", "-", "*", "/"};

        Spinner spin = (Spinner) findViewById(R.id.spinner_operacoes);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, operacoes);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(adapter);

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

                pos = spin.getSelectedItemPosition();
                //teste
                Toast.makeText(getApplicationContext(), + pos, Toast.LENGTH_SHORT).show();
            }

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

            }
        });
    }

1 answer

0


In case someone is going through the same problem, I solved it as follows:

Instead of int with the position I returned the position value for a string. And the list populating my spinner had to be declared as final so that it does not give problems of an inner class calling the same variable.

After these brief changes it worked as expected:

public class desafio extends AppCompatActivity {

EditText campo1, campo2;
String selected;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_desafio);

    final String[] operacoes = {"", "+", "-", "*", "/"};
    Spinner spin = findViewById(R.id.spinner_operacoes);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, operacoes);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(adapter);

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

            selected = operacoes[position];
            //teste
            Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
        }

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

        }
    });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.