0
Speak people, I’m trying to get the id or position inside an Arrayview of the Spinner component but it’s not getting inside the variable. If I place a Toast to display the id or position, Toast displays normally. I’m wanting to store 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();
}
}
That one
if/else
is where?– ramaral
I created a "calculate" method and put it in, outside onCreate. When I click on the Result button, it calls the "calculate" method".
– Flávio
position = position
– itscorey
@Luc He says Toast displays the value correctly.
– ramaral
@ramaral, I mean, it calls onItemSelected that gives it the position of the item, then, in the same method, it does a search in the spinner to know the position of the selected item. It’s kind of redundant, isn’t it? Or the position of the method is different from that of the spinner?
– itscorey
@Luc Yes this is true, it is redundant. However do
posição = position
will not solve the problem sinceposition
is equal togetSelectedItemPosition()
. Note that he usesposicao
in Toast and it says it displays the correct value.– ramaral
Yes... it’s strange. : the
– itscorey
I made the following personal change: In Mainactivity I placed Implements Adapterview.Onitemselectedlistener, creating two public methods.(onItemSelected and onNothingSelected). Within the onItemSelected method I placed the position variable receiving the method ID parameter. Yes, I was able to get the selection inside the spinner and doing the if/Else according to the result of the position. I’ve also added onCreate , Adapter.setDropnViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerIdade.setOnItemSelectedListener(this); I’ve removed Adapterview.Onitemselectedlistener.
– Flávio