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();
}
}