I want to save and recover the selected value in a Spinner in SharedPreferences

1

I can bring the values to other components but not to the Spinner , this is my code:

//Función que crea un archivo de preferencias
public void CargarPreferencia()
{
    SharedPreferences mispreferencias=getSharedPreferences("PrefeM", Context.MODE_PRIVATE);

    tecnicoMS.setText(mispreferencias.getString("tecnicoM",""));
    txtdate.setText(mispreferencias.getString("dateM",""));
    cliente.setText(mispreferencias.getString("clienteM",""));
    txtsucursal.setText(mispreferencias.getString("sucursalM",""));
}

//Función que guarda el estado del CheckBox para saber si se mandó el SMS y no volver a enviarlo
public void GuardarPreferencias()
{
//        String txtsel=tecnico.getSelectedItem().toString();

//        String tecselec= (String) tecnico.getSelectedItem();
//        String cliselec= (String) cliente.getSelectedItem();

    SharedPreferences mispreferencias=getSharedPreferences("PrefeM",Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = mispreferencias.edit();

    String valor10 = tecnico.getSelectedItem().toString();
    String valor20 = txtdate.getText().toString();
    String valor30 = cliente.getSelectedItem().toString();
    String valor40 = txtsucursal.getText().toString();

    editor.putString("tecnicoM",valor10);
    editor.putString("dateM",valor20);
    editor.putString("clienteM",valor30);
    editor.putString("sucursalM",valor40);
    editor.commit();

}
    
asked by Gustavo Giuffre 26.11.2017 в 13:39
source

1 answer

2

To save the position of a item from Spinner to SharedPreference , use the putInt() of SharedPreference . Then to get the value of the SharedPreference , use the method getInt() of it.

int valor = tecnico.getSelectedItem(); 
editor.putInt("posicion", valor);

To select the position of the item use the method setSelection() of Spinner .

tecnico.setSelection(mispreferencias.getInt("posicion", 0));
    
answered by 26.11.2017 / 17:58
source