Android - How do I set a selected item from a Spinner to a string?

1

I have a question in my code, I'm trying to get the value of Spinner and write it to the bank, but I can not get the spinner to have String value. In case it is giving me error at the setText time, at the end of the code. "Can not resolve method 'setText (java.lang.String)'"

public class editarContato extends Activity {

Spinner spinner; String[] arraySpinner;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editar_contato);

spinner = (Spinner) findViewById(R.id.spinner);
this.arraySpinner = new String[] {"SP", "RJ", "SC"};

ArrayAdapter<String> adapter = new ArrayAdapter<String>
    (this, android.R.layout.simple_spinner_item, arraySpinner);
spinner.setAdapter(adapter);

btAtualizar   =  (Button) findViewById(R.id.atualizar);

carregaDetalhesContato();

btAtualizar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            datasource = new DBAdapter(editarContato.this);
            datasource.open();
            contato = datasource.getContato(idContato);
            datasource.close();
            datasource.AlterarContato(spinner.getSelectedItem().toString());
            datasource.close();
}
});

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView parent,View view, int pos, long id) {
            Object item = parent.getItemAtPosition(pos);
        }
        public void onNothingSelected(AdapterView parent){}
    });
}

public void carregaDetalhesContato() {

    idContato = getIntent().getIntExtra("id", 0);

    datasource = new DBAdapter(this);
    datasource.open();
    contato = datasource.getContato(idContato);
    datasource.close();

spinner.setText(contato.getSpinner());
}

}
    
asked by anonymous 02.06.2016 / 05:10

1 answer

2

The Spinner class does not have the setText method. For this you can use the setSelection(int index) method. For example, select selection for SP:

spinner.setSelection(adapter.getPosition("SP"))
    
02.06.2016 / 14:33