How to set a value on a Spinners?

Asked

Viewed 3,920 times

1

I have a formulariohelper which obtains the value of Spinners and save to the bank, but I can’t load the value of the bank into Spinners. Like I set the value?

public Aluno getAluno(){
    aluno.setNome(nome.getText().toString());
    Log.i(TAG,"helper: "+ aluno.getNome());
    aluno.setSobrenome(sobrenome.getText().toString());
    Log.i(TAG,"helper: "+ aluno.getSobrenome());
    aluno.setEmail(email.getText().toString());
    Log.i(TAG,"helper: "+ aluno.getEmail());

   //aqui ele pegar o valor da spinner
    aluno.setSexo(sexo.getOnItemSelectedListener().toString());
    Log.i(TAG,"helper: "+ aluno.getSexo());
    aluno.setEmpresa(empresa.getSelectedItem().toString());
    Log.i(TAG,"helper: "+ aluno.getEmpresa());
    aluno.setNit(nit.getText().toString());
    Log.i(TAG,"helper: "+ aluno.getNit());
    aluno.setDatanascimento(datanascimento.getText().toString());
    Log.i(TAG,"helper: "+ aluno.getDatanascimento());
    aluno.setAltura(altura.getText().toString());
    Log.i(TAG,"helper: "+ aluno.getAltura());
    aluno.setPeso(peso.getText().toString());
    Log.i(TAG,"helper: "+ aluno.getPeso());

    aluno.setImc(imc.getText().toString());
    Log.i(TAG,"helper: "+ aluno.getImc());
    return aluno;

}
public void setAluno(Aluno aluno){

    nome.setText(aluno.getNome());
    Log.i(TAG,"Helper setAluno: "+ aluno.getNome());
    sobrenome.setText(aluno.getSobrenome());
    Log.i(TAG,"Helper setAluno: "+ aluno.getSobrenome());

    //aqui ele seta o valor pra carregar mas nao consegue
    sexo.setTag(aluno.getSexo());
    Log.i(TAG,"Helper setAluno: "+ aluno.getSexo());
    empresa.setTag(aluno.getEmpresa());
    email.setText(aluno.getEmail());
    Log.i(TAG,"Helper setAluno: "+ aluno.getEmail());
    Log.i(TAG,"Helper setAluno: "+ aluno.getEmpresa());
    datanascimento.setText(aluno.getDatanascimento());
    Log.i(TAG,"Helper setAluno: "+ aluno.getDatanascimento());
    nit.setText(aluno.getNit());
    Log.i(TAG,"Helper setAluno: "+ aluno.getNit());
  • You want to know how to set the selected item in the Spinner?

  • you mean through setOnItemSelectedListener?

  • I don’t understand. What exactly do you want to do?

  • I want to take a value from the bank and press the spinner.

  • I don’t understand what your problem is.

2 answers

1

Friend if I understand correctly. You want a form already open with the Spinner filled with the value saved in the Database.

For this after filling your Arraylist that will popular the Basedapter or Arrayadapter with the list of all the items of the Database in the desired spinner put the code below:

String valorBanco = "Valor do Banco que deve ser selecionado no Spinner";
int posicaoArray = 0;

for(int i=0; (i <= seuArrayList.size()-1) ; i++){

   if(seuArrayList.get(i).equals(valorBanco)){

       posicaoArray = i;
       break;
   }else{
       posicaoArray = 0;
   }
}
   seuSpinner.setSelection(posicaoArray);
}

0

Suppose your Spinner is named Myspinner, and it contains as one of your choices: "some bank value".

To find and compare the position of "some bank value" in Spinner use this:

//Dentro desta variável conteria o valor do banco, abaixo um exemplo
String compareValue= "algum valor do banco";

//Criando o adapter
    ArrayAdapter<CharSequence> adapter= ArrayAdapter.createFromResource(this, R.array.select_state, android.R.layout.simple_spinner_item);

//Setando o valor
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    MySpinner.setAdapter(adapter);

    if (!compareValue.equals(null)) {
        int spinnerPostion = adapter.getPosition(compareValue);
        MySpinner.setSelection(spinnerPostion);
        spinnerPostion = 0;
    }

Source

Browser other questions tagged

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