Error picking ID or Position inside a Spinner

Asked

Viewed 209 times

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?

  • I created a "calculate" method and put it in, outside onCreate. When I click on the Result button, it calls the "calculate" method".

  • position = position

  • @Luc He says Toast displays the value correctly.

  • @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?

  • @Luc Yes this is true, it is redundant. However do posição = position will not solve the problem since position is equal to getSelectedItemPosition(). Note that he uses posicao in Toast and it says it displays the correct value.

  • Yes... it’s strange. : the

  • 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.

Show 3 more comments

1 answer

0


I see no reason for that to happen.
However, if the reason of AdapterView.OnItemSelectedListener is only for "set" the attribute posicao, to then know which item is selected in Spinner elsewhere, you do not need to do so.
The method getSelectedItemPosition() 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();
    }
}

Browser other questions tagged

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