我有一个活动,其中y有3个搜索栏,我在每个搜索栏的文本视图中看到每个搜索栏的值。
现在,我想用sharedPreferences存储每个搜索栏的值。我已经尝试过几种方式,但我无法得到它。
我已经知道这样做的正确方法是在调用onPause时保存值,并在调用onResume时恢复它们。
public class Configuration extends Activity implements OnSeekBarChangeListener {
public TextView progress1;
public TextView progress2;
public TextView progress3;
private SeekBar bar1;
private SeekBar bar2;
private SeekBar bar3;
private int savedProgress1;
private int savedProgress2;
private int savedProgress3;
private int loadedProgress1;
private int loadedProgress2;
private int loadedProgress3;
private String Key_PROGRESS_1 = "bar1_progress";
private String Key_PROGRESS_2 = "bar2_progress";
private String Key_PROGRESS_3 = "bar3_progress";
SharedPreferences mPreferences;
SharedPreferences.Editor editor;
/*********************
*
* ONCREATE
*
********************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configuracion2);
bar1 = (SeekBar)findViewById(R.id.fast_bar); //make seekbar object
bar1.setOnSeekBarChangeListener(this); //set seekbar listener
progress1 = (TextView)findViewById(R.id.salida1);
bar2 = (SeekBar)findViewById(R.id.climb_bar);
bar2.setOnSeekBarChangeListener(this);
progress2 = (TextView)findViewById(R.id.salida2);
bar3 = (SeekBar)findViewById(R.id.platform_bar);
bar3.setOnSeekBarChangeListener(this);
progress3 = (TextView)findViewById(R.id.salida3);
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = mPreferences.edit();
}
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
Log.v("", "" + bar);
switch (bar.getId()) {
case R.id.fast_bar:
progress1.setText(progress + "%");
break;
case R.id.climb_bar:
progress2.setText(progress + "%");
break;
case R.id.platform_bar:
progress3.setText(progress + "%");
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setSecondaryProgress(seekBar.getProgress());
}
protected void onResume() {
super.onResume();
loadedProgress1 = mPreferences.getInt("Key_PROGRESS_1", 0);
bar1.setProgress(loadedProgress1);
loadedProgress2 = mPreferences.getInt("Key_PROGRESS_2", 0);
bar2.setProgress(loadedProgress2);
loadedProgress2 = mPreferences.getInt("Key_PROGRESS_3", 0);
bar3.setProgress(loadedProgress3);
}
protected void onPause(/*SeekBar seekBar*/) {
super.onPause();
savedProgress1 = bar1.getProgress();
editor.putInt(Key_PROGRESS_1, savedProgress1);
savedProgress2 = bar2.getProgress();
editor.putInt(Key_PROGRESS_2, savedProgress2);
savedProgress3 = bar3.getProgress();
editor.putInt(Key_PROGRESS_3, savedProgress3);
editor.commit();
}
}
答案 0 :(得分:1)
不要将同一个SharedPreferences.Editor
对象保留为私有成员。
在mPreferences.edit()
内致电onPause()
:
SharedPreferences.Editor editor = mPreferences.edit();
savedProgress1 = bar1.getProgress();
editor.putInt(Key_PROGRESS_1, savedProgress1);
savedProgress2 = bar2.getProgress();
editor.putInt(Key_PROGRESS_2, savedProgress2);
savedProgress3 = bar3.getProgress();
editor.putInt(Key_PROGRESS_3, savedProgress3);
editor.commit();
此外,它无法正常工作,因为您在"Key_PROGRESS_X"
方法中使用字符串Key_PROGRESS_X
而不是变量onResume()
。