问题
I am able to save Strings in my saved preferences but having difficulty saving my spinner.
public class Diet extends Activity {
private SharedPreferences sharedPreferences;
Spinner spnCalorieRange;
Here is my onCreate:
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String strAge = Integer.toString(age);
String strHeight = Integer.toString(height);
String strWeight = Integer.toString(weight);
name = loadSavedPreference("name");
strAge = loadSavedPreference("strAge");
strHeight = loadSavedPreference("strHeight");
strWeight = loadSavedPreference("strWeight");
etName.setText(name);
etAge.setText(strAge);
etHeight.setText(strHeight);
etWeight.setText(strWeight);
This is my Spinner in my onCreate:
spinner = (Spinner)findViewById(R.id.spnCalorieRange);
adapter = ArrayAdapter.createFromResource(this, R.array.Calorie_Range, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
long item = parent.getItemIdAtPosition(position);
String pos =spinner.getSelectedItem().toString();
//sharedPreferences.edit().putInt("PREF_SPINNER", position).commit();
if (item == 0){
deficitPercentage = .05;
}
else if (item ==1)
{
deficitPercentage = .1;
}
else if (item ==2)
{
deficitPercentage = .15;
}
else if (item ==3)
{
deficitPercentage = .2;
}
else if (item ==4)
{
deficitPercentage = .25;
}
else
{
deficitPercentage = .3;
}
//editor.putString("pos", pos);
//editor.commit();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
This is in my onCLick behind a button, where I'm saving the strings and spinner
age = (int) Double.parseDouble(etAge.getText().toString());
height = (int) Double.parseDouble(etHeight.getText().toString());
weight = (int) Double.parseDouble(etWeight.getText().toString());
//Save Preferences
String strAge = Integer.toString(age);
String strHeight = Integer.toString(height);
String strWeight = Integer.toString(weight);
name = etName.getText().toString();
savePreference("name",name);
strAge = etAge.getText().toString();
savePreference("strAge",strAge);
strHeight = etHeight.getText().toString();
savePreference("strHeight",strHeight);
strWeight = etWeight.getText().toString();
savePreference("strWeight",strWeight);:
回答1:
You can't save the actual spinner object to shared prefs, but you can save all of the values that make the spinner work its magic and next time you create the spinner, just apply those values
回答2:
In your onItemSelected
place:
int spinnerPosition = spinner.getSelectedItemPosition();
saveSpinnerPosition(spinnerPosition);
Save method
public void saveSpinnerPosition(int position){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
prefEditor.putInt("spnCalorieRange",position);
prefEditor.apply();
}
Load method
public void loadSpinnerPosition{
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int position= sharedPreferences .getInt"spnCalorieRange",-1);
if(spinnerValue > -1)
// set the value of the spinner
spinner.setSelection(position);
}
To put spinners back to position, call loadSpinnerPosition
in your onCreate
------------------------------------------------------------------------------------------------------------------------------------
Edit:
because I noticed this post is also your question, u can also do everything at once:
At the top of your activity:
int spinnerPosition;
In your onItemSelected
place:
spinnerPosition = spinner.getSelectedItemPosition();
Save method
public void saveMethod(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("Gender", radioSexButton.isChecked());
editor.putBoolean("Male", rdoMale.isChecked());
prefEditor.putInt("spnCalorieRange",spinnerPosition);
prefEditor.apply();
}
Load method
public void loadMethod(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int position= sharedPreferences .getInt"spnCalorieRange",-1);
radioSexButton.setChecked(sharedPreferences.getBoolean("Gender", false));
rdoMale.setChecked(sharedPreferences.getBoolean("Male", false));
if(spinnerValue > -1)
// set the value of the spinner
spinner.setSelection(position);
}
Then call saveMethod()
when u want to save all your states, and call loadMethod()
when u wanna load all your states
Should help u out
来源:https://stackoverflow.com/questions/35604579/how-to-save-spinner-to-saved-shared-preferences