Error getting ID or Position inside a Spinner

0

Personally speaking, I'm trying to get the id or the position inside an ArrayView of the Spinner component but it is not getting inside the variable. If I put a Toast to display the id or position, the Toast normally displays. I'm trying to save it inside this variable so I can test the value in one (if and else) and show the content according to what the user chooses in the spinner.

private Spinner spinnerIdade; 
private int posicao;    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

spinnerIdade = (Spinner) findViewById(R.id.spinner_Idade);
     ArrayAdapter adapter = ArrayAdapter.createFromResource(MainActivity.this,
            R.array.spinner_idade, android.R.layout.simple_list_item_activated_1);

    spinnerIdade.setAdapter(adapter);
 AdapterView.OnItemSelectedListener escolha = new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        posicao = spinnerIdade.getSelectedItemPosition();
        //Toast.makeText(MainActivity.this,"ID: " + posicao, Toast.LENGTH_LONG).show();

    }

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

    }
};
    spinnerIdade.setOnItemSelectedListener(escolha);

And the IF Else to take the test

        if(posicao == 0) {
            int x = Integer.parseInt(idade.getText().toString()) - 15;
            int y = (Integer.parseInt(salario.getText().toString()) * x) / 100;
        } else if (posicao == 1) {
            int x = Integer.parseInt(idade.getText().toString()) - 10;
            int y = (Integer.parseInt(salario.getText().toString()) * x) / 100;
        } else if (posicao == 2) {
             int x = Integer.parseInt(idade.getText().toString()) - 15;
            int y = (Integer.parseInt(salario.getText().toString()) * 50) / 100;
        } else {
            Toast.makeText(MainActivity.this, "Verifique as opções e tente novamente",
                    Toast.LENGTH_SHORT).show();
        }
    }
    
asked by anonymous 24.05.2017 / 14:07

1 answer

0

I see no reason for this to happen. However, if the ratio of AdapterView.OnItemSelectedListener is just to "set" the posicao attribute, then you know what the selected item in Spinner is in another location, you do not need to do so. The getSelectedItemPosition() method can be used for this purpose.

So, instead of an attribute, declare in the method calcula a variable and use it in if's

    ...
    int posicao = spinnerIdade.getSelectedItemPosition();
    if(posicao == 0) {
        int x = Integer.parseInt(idade.getText().toString()) - 15;
        int y = (Integer.parseInt(salario.getText().toString()) * x) / 100;
    } else if (posicao == 1) {
        int x = Integer.parseInt(idade.getText().toString()) - 10;
        int y = (Integer.parseInt(salario.getText().toString()) * x) / 100;
    } else if (posicao == 2) {
         int x = Integer.parseInt(idade.getText().toString()) - 15;
        int y = (Integer.parseInt(salario.getText().toString()) * 50) / 100;
    } else {
        Toast.makeText(MainActivity.this, "Verifique as opções e tente novamente",
                Toast.LENGTH_SHORT).show();
    }
}
    
24.05.2017 / 14:25