本文介绍了如何微调保存为共享preference?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经能够整数和字符串保存为共享preferences但搜查,似乎无法能够选择的值保存纱厂为共享preference?
=微调(微调)findViewById(R.id.spnCalorieRange);
适配器= ArrayAdapter.createFromResource(这一点,R.array.Calorie_Range,android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(适配器); spinner.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){ @覆盖
公共无效onItemSelected(适配器视图<>母公司,观景,
INT位置,长的id){
长项= parent.getItemIdAtPosition(位置);
。字符串POS = spinner.getSelectedItem()的toString();
//shared$p$pferences.edit().putInt(\"$p$pF_SPINNER位置).commit();
如果(项目== 0){
deficitPercentage = 0.05;
}
否则如果(项目== 1)
{
deficitPercentage = 0.1;
}
否则如果(项目== 2)
{
deficitPercentage = 0.15;
}
否则,如果(项目== 3)
{
deficitPercentage = 0.2;
}
否则,如果(项目== 4)
{
deficitPercentage = 0.25;
}
其他
{
deficitPercentage = 0.3;
}
editor.putString(POS,POS机);
editor.commit();
} @覆盖
公共无效onNothingSelected(适配器视图<>母公司){
// TODO自动生成方法存根 } });
在这里访问共享preferences在OnCreate中:
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_diet);
spnCalorieRange =(微调)findViewById(R.id.spnCalorieRange); 共享preferences = preferenceManager.getDefaultShared preferences(本);
。共享preferences preF = getApplicationContext()getShared preferences(选项,MODE_PRIVATE);
编辑= pref.edit(); 字符串strAvgCalIntake = Double.toString(dailyCalorieIntake);
字符串strGoal = Double.toString(目标); strAvgCalIntake = loadSaved preference(strAvgCalIntake);
strGoal = loadSaved preference(strGoal); etAverageCalorieIntake.setText(strAvgCalIntake);
etLoseWeight.setText(strGoal); //米preFS = getShared preferences(preFS_NAME,MODE_PRIVATE);
//spinner.setSelection(m$p$pfs.getInt($p$pF_SPINNER,0)); // INT selectedPosition =共享preferences.getInt(spinnerSelection,0);
INT selectedPosition = spnCalorieRange.getSelectedItemPosition();
共享preferences.getInt(spinnerSelection,selectedPosition);
((编辑)共享preferences).commit();
下面就是我节省的共享preferences按钮:
字符串是BTW节约罚款。
按钮btnBack =(按钮)findViewById(R.id.btnBack);
btnBack.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){ 字符串strAvgCalIntake = Double.toString(dailyCalorieIntake);
字符串strGoal = loadSaved preference(strGoal);
spnCalorieRange =(微调)findViewById(R.id.spnCalorieRange); 。strAvgCalIntake = etAverageCalorieIntake.getText()的toString();
节省preference(strAvgCalIntake,strAvgCalIntake); 。strGoal = etLoseWeight.getText()的toString();
节省preference(strGoal,strGoal); 共享preferences US preF = getShared preferences(preF,Activity.MODE_PRIVATE);
共享preferences.Editor编辑= S pref.edit();
editor.putString(deficitPercentage_key,Double.toString(deficitPercentage)); //
editor.commit();
解决方案
如果您已经验证了 deficitPercentage
值,(我希望你做了)
共享preferences US preF = getShared preferences(your_ prefs_name,Activity.MODE_PRIVATE);
共享preferences.Editor编辑= S pref.edit();
editor.putString(deficitPercentage_key,Double.toString(deficitPercentage)); //
editor.commit();
I have been able to save Integers and Strings as Shared Preferences but have searched and cannot seem to be able to save a Spinners selected value as a shared preference?
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
}
});
Accessing Shared Preferences here in OnCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diet);
spnCalorieRange = (Spinner) findViewById(R.id.spnCalorieRange);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences pref = getApplicationContext().getSharedPreferences("Options", MODE_PRIVATE);
editor=pref.edit();
String strAvgCalIntake = Double.toString(dailyCalorieIntake);
String strGoal = Double.toString(goal);
strAvgCalIntake = loadSavedPreference("strAvgCalIntake");
strGoal = loadSavedPreference("strGoal");
etAverageCalorieIntake.setText(strAvgCalIntake);
etLoseWeight.setText(strGoal);
//mPrefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
//spinner.setSelection(mPrefs.getInt(PREF_SPINNER, 0));
// int selectedPosition = sharedpreferences.getInt("spinnerSelection", 0);
int selectedPosition = spnCalorieRange.getSelectedItemPosition();
sharedPreferences.getInt("spinnerSelection", selectedPosition);
((Editor) sharedPreferences).commit();
Here is the Button where I'm saving the shared Preferences:The Strings are saving fine btw.
Button btnBack = (Button)findViewById(R.id.btnBack);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strAvgCalIntake = Double.toString(dailyCalorieIntake);
String strGoal = loadSavedPreference("strGoal");
spnCalorieRange = (Spinner) findViewById(R.id.spnCalorieRange);
strAvgCalIntake = etAverageCalorieIntake.getText().toString();
savePreference("strAvgCalIntake",strAvgCalIntake);
strGoal = etLoseWeight.getText().toString();
savePreference("strGoal",strGoal);
SharedPreferences spref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = spref.edit();
editor.putString("deficitPercentage_key", Double.toString(deficitPercentage)); //
editor.commit();
解决方案
If you have validated the deficitPercentage
value, (and I hope you did)
SharedPreferences spref = getSharedPreferences("your_prefs_name", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = spref.edit();
editor.putString("deficitPercentage_key", Double.toString(deficitPercentage)); //
editor.commit();
这篇关于如何微调保存为共享preference?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!