I have a spinner. I want to save its value in saveFile()
and retrieve the same on calling readFile()
using sharedpreferences. I haven't got the right way to do it yet.
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.countries_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
public void saveFile(){
SharedPreferences sharedPref = getSharedPreferences(FileName,Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPref.edit();
}
public void readFile(){
SharedPreferences sharedPref = getSharedPreferences(FileName,Context.MODE_PRIVATE);
}
Try this
public void saveFile(){
SharedPreferences sharedPref = getSharedPreferences(FileName,Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPref.edit();
editor.putInt("spinnerValue",spinner.getSelectedItemPosition());
editor.apply();
}
public void readFile(){
SharedPreferences sharedPref = getSharedPreferences(FileName,Context.MODE_PRIVATE);
int index = sharedPref.getInt("spinnerValue",0);
spinner.setSelection(index);
}