这是我保存数据的代码:
private void Save_it() {
sharedPreferences = getPreferences(MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putInt(SAVED_HOUR_1, selected_hour_1);
editor.putInt(SAVED_MINUTE_1, selected_minute_1);
editor.putInt(SAVED_VOLUME_1, selected_volume_1);
editor.putString(SAVED_TIME_TEXT_1, Name_Time_1);
editor.putString(SAVED_VOLUME_TEXT_1, Name_Volume_1);
editor.putBoolean(CHECK_IF_SET_TIME_1, checktime_1);
editor.putBoolean(CHECK_IF_SET_VOLUME_1, checkvolume_1);
editor.putBoolean(CHECK_IF_SET_1, checkset_1);
editor.putInt(SAVED_HOUR_2, selected_hour_2);
editor.putInt(SAVED_MINUTE_2, selected_minute_2);
editor.putInt(SAVED_VOLUME_2, selected_volume_2);
editor.putString(SAVED_TIME_TEXT_2, Name_Time_2);
editor.putString(SAVED_VOLUME_TEXT_2, Name_Volume_2);
editor.putBoolean(CHECK_IF_SET_TIME_2, checktime_2);
editor.putBoolean(CHECK_IF_SET_VOLUME_2, checkvolume_2);
editor.putBoolean(CHECK_IF_SET_2, checkset_2);
editor.putBoolean(CHECK_IF_ENABLED_2, checkenabled_2);
editor.apply();
}
这是我的加载数据代码:
private void Load_it() {
sharedPreferences = getPreferences(MODE_PRIVATE);
String Saved_Time_Text_1 = sharedPreferences.getString(SAVED_TIME_TEXT_1, Name_Time_1);
set_time_1 = (TextView)findViewById(R.id.text_time_1);
set_time_1.setText(Saved_Time_Text_1);
String Saved_Volume_Text_1 = sharedPreferences.getString(SAVED_VOLUME_TEXT_1, Name_Volume_1);
set_volume_1 = (TextView)findViewById(R.id.text_volume_1);
set_volume_1.setText(Saved_Volume_Text_1);
selected_hour_1 = sharedPreferences.getInt(SAVED_HOUR_1, 0);
selected_minute_1 = sharedPreferences.getInt(SAVED_MINUTE_1, 0);
selected_volume_1 = sharedPreferences.getInt(SAVED_VOLUME_1, 0);
checktime_1 = sharedPreferences.getBoolean(CHECK_IF_SET_TIME_1, false);
checkvolume_1 = sharedPreferences.getBoolean(CHECK_IF_SET_VOLUME_1, false);
checkset_1 = sharedPreferences.getBoolean(CHECK_IF_SET_1, false);
String Saved_Time_Text_2 = sharedPreferences.getString(SAVED_TIME_TEXT_2, Name_Time_2);
set_time_2 = (TextView)findViewById(R.id.text_time_2);
set_time_2.setText(Saved_Time_Text_2);
String Saved_Volume_Text_2 = sharedPreferences.getString(SAVED_VOLUME_TEXT_2, Name_Volume_2);
set_volume_2 = (TextView)findViewById(R.id.text_volume_2);
set_volume_2.setText(Saved_Volume_Text_2);
selected_hour_2 = sharedPreferences.getInt(SAVED_HOUR_2, 0);
selected_minute_2 = sharedPreferences.getInt(SAVED_MINUTE_2, 0);
selected_volume_2 = sharedPreferences.getInt(SAVED_VOLUME_2, 0);
checktime_2 = sharedPreferences.getBoolean(CHECK_IF_SET_TIME_2, false);
checkvolume_2 = sharedPreferences.getBoolean(CHECK_IF_SET_VOLUME_2, false);
checkset_2 = sharedPreferences.getBoolean(CHECK_IF_SET_2, false);
checkenabled_2 = sharedPreferences.getBoolean(CHECK_IF_ENABLED_2, false);
}
但是负载不能正常工作。我在textView中使用setText的数据并设置boolean和integer变量。我想加载代码中的问题:我使用它不对。怎么了?救命!! 在这里编辑:goo.gl/fhIxdv
答案 0 :(得分:0)
请看这篇文章,还看看我的答案;我已经展示了一种存储和获取偏好的正确方法。 Click here
尝试类似
的内容private String DEFAULT_SP_KEY = "default_sp_key";
private void Save_it(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(DEFAULT_SP_KEY, 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(SAVED_HOUR_1, selected_hour_1);
editor.putInt(SAVED_MINUTE_1, selected_minute_1);
editor.putInt(SAVED_VOLUME_1, selected_volume_1);
editor.putString(SAVED_TIME_TEXT_1, Name_Time_1);
editor.putString(SAVED_VOLUME_TEXT_1, Name_Volume_1);
editor.putBoolean(CHECK_IF_SET_TIME_1, checktime_1);
editor.putBoolean(CHECK_IF_SET_VOLUME_1, checkvolume_1);
editor.putBoolean(CHECK_IF_SET_1, checkset_1);
editor.putInt(SAVED_HOUR_2, selected_hour_2);
editor.putInt(SAVED_MINUTE_2, selected_minute_2);
editor.putInt(SAVED_VOLUME_2, selected_volume_2);
editor.putString(SAVED_TIME_TEXT_2, Name_Time_2);
editor.putString(SAVED_VOLUME_TEXT_2, Name_Volume_2);
editor.putBoolean(CHECK_IF_SET_TIME_2, checktime_2);
editor.putBoolean(CHECK_IF_SET_VOLUME_2, checkvolume_2);
editor.putBoolean(CHECK_IF_SET_2, checkset_2);
editor.putBoolean(CHECK_IF_ENABLED_2, checkenabled_2);
editor.apply();
}
private void Load_it(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(DEFAULT_SP_KEY, 0);
String Saved_Time_Text_1 = sharedPreferences.getString(SAVED_TIME_TEXT_1, Name_Time_1);
set_time_1 = (TextView)findViewById(R.id.text_time_1);
set_time_1.setText(Saved_Time_Text_1);
String Saved_Volume_Text_1 = sharedPreferences.getString(SAVED_VOLUME_TEXT_1, Name_Volume_1);
set_volume_1 = (TextView)findViewById(R.id.text_volume_1);
set_volume_1.setText(Saved_Volume_Text_1);
selected_hour_1 = sharedPreferences.getInt(SAVED_HOUR_1, 0);
selected_minute_1 = sharedPreferences.getInt(SAVED_MINUTE_1, 0);
selected_volume_1 = sharedPreferences.getInt(SAVED_VOLUME_1, 0);
checktime_1 = sharedPreferences.getBoolean(CHECK_IF_SET_TIME_1, false);
checkvolume_1 = sharedPreferences.getBoolean(CHECK_IF_SET_VOLUME_1, false);
checkset_1 = sharedPreferences.getBoolean(CHECK_IF_SET_1, false);
String Saved_Time_Text_2 = sharedPreferences.getString(SAVED_TIME_TEXT_2, Name_Time_2);
set_time_2 = (TextView)findViewById(R.id.text_time_2);
set_time_2.setText(Saved_Time_Text_2);
String Saved_Volume_Text_2 = sharedPreferences.getString(SAVED_VOLUME_TEXT_2, Name_Volume_2);
set_volume_2 = (TextView)findViewById(R.id.text_volume_2);
set_volume_2.setText(Saved_Volume_Text_2);
selected_hour_2 = sharedPreferences.getInt(SAVED_HOUR_2, 0);
selected_minute_2 = sharedPreferences.getInt(SAVED_MINUTE_2, 0);
selected_volume_2 = sharedPreferences.getInt(SAVED_VOLUME_2, 0);
checktime_2 = sharedPreferences.getBoolean(CHECK_IF_SET_TIME_2, false);
checkvolume_2 = sharedPreferences.getBoolean(CHECK_IF_SET_VOLUME_2, false);
checkset_2 = sharedPreferences.getBoolean(CHECK_IF_SET_2, false);
checkenabled_2 = sharedPreferences.getBoolean(CHECK_IF_ENABLED_2, false);
}