How do I set a value on a Spinners?

1

I have a formhelper that gets the value of Spinners and writes to the bank, but I can not load the bank's value into Spinners. How do 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());
    
asked by anonymous 06.03.2015 / 19:34

2 answers

1

Friend if I understood correctly. You want a form to already open with Spinner filled with the value recorded in the Database.

To do this after filling in your ArrayList that will populate the BaseAdapter or ArrayAdapter with the list of all the database items 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);
}
    
20.12.2016 / 03:42
0

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

To find and compare the "some bank value" position 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

    
09.03.2015 / 00:53