estoy desarrollando una aplicación la cual tiene unos botones switch, los cuales tienen SharedPreferences
para que cuando se cierre la aplicación y se vuelva a abrir, los botones previamente activados, lo sigan estando.
SharedPreferences en la clase MainActivity:
private Spinner spipol;
private Switch quim, fil, fis, tri, esp, engl, inf, eti, reli, est, pol, dib, edf, mate;
private SharedPreferences sharedPreferences, sharfil, sharquim, shardfis, shartrig, sharesp, sharemater, sharing, sharinf, shareti, sharrel, sharest, sharpol, sharedf;
public static final String ex1 = "switch1";
public static final String ex = "switch";
dib = (Switch) findViewById(R.id.switch1);
sharedPreferences = getSharedPreferences("dib", MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPreferences.edit();
dib.setChecked(sharedPreferences.getBoolean(ex, false));
dib.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
editor.putBoolean(ex, true);
} else {
editor.putBoolean(ex, false);
}
editor.commit();
}
});
Lo que necesito es saber como puedo hacer algo parecido a lo que tengo con los botones switch, pero con mi spinner:
String [] opciones = {"","Tarea corta","Taller","Estudiar","Preguntas","Repasar"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, opciones);
spipol.setAdapter(adapter);
}
Busque en internet como hacerlo, pero en todos los lugares que veia, usaban un botón para guardar, lo que me gustaría saber es si puedo hacer esto sin necesidad de la implementación de este botón.
Edit: Probé esto pero no funciono:
spipol.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
CapturaSpinner = (String) parent.getItemAtPosition(position);
index = position;
System.out.println("Indice:"+index);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void guardarPreference(Context context, int index) {
SharedPreferences sharpref = getPreferences(getApplicationContext().MODE_PRIVATE);
SharedPreferences.Editor editor = sharpref.edit();
editor.putInt("Dato", index);
System.out.println("Indice:"+index);
editor.apply();
}
}
Aqui intente declara la variable con la ayuda que brinda android studio, pero no funciono.
Esto aparece en el Build: error: illegal start of expression public void obtenerPreference (Context Context context;