我正在尝试将字符串设置为共享偏好设置,乍一看似乎有效。在应用程序的其他部分,我可以访问共享首选项并正确读取字符串集。
当我离开应用程序时出现问题。来自共享首选项字符串集的所有数据都将丢失,并再次返回空集。
我可以在应用程序关闭并重新打开之前访问它,这让我觉得它存储在内存中但没有存储到磁盘中。
我在这里阅读了很多答案,尝试在提交和申请之间进行更改,但我不知道导致问题的原因。
我尝试保存它的方式是:
以下是代码:
public static void storeReminder (Context context, String reminderString){
// Get the set of reminder strings
SharedPreferences sharedPreferences = context.getSharedPreferences("AppData", Context.MODE_PRIVATE);
Set <String> remindersStringSet = sharedPreferences.getStringSet(context.getResources().getString(R.string.reminders_hashset_key), new HashSet<String>());
// Add the new reminder string to the reminder string set
remindersStringSet.add(reminderString);
// Save the reminder string set now that the new reminder string has been added
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet(context.getResources().getString(R.string.reminders_hashset_key), remindersStringSet);
editor.commit();
}
这就是我在应用程序的其他部分获取存储的hashset的方法:
// Get the set of reminder strings
SharedPreferences sharedPreferences = context.getSharedPreferences("AppData", Context.MODE_PRIVATE);
Set<String> remindersStringSet = sharedPreferences.getStringSet(context.getResources().getString(R.string.reminders_hashset_key), new HashSet<String>());
提前感谢您的帮助
答案 0 :(得分:0)
确保在打开app等登录或其他方法时无法清除共享首选项
答案 1 :(得分:0)
创建此类:
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import java.util.Set;
public class DemoPrefs {
private SharedPreferences prefs;
private SharedPreferences.Editor prefs_edit;
private static DemoPrefs instance;
public DemoPrefs(Context context) {
initialize(context);
}
private void initialize(Context context) {
prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs_edit = prefs.edit();
}
public static DemoPrefs getInstance(Context context) {
if (instance == null) {
instance = new DemoPrefs(context);
}
return instance;
}
public void setReminderSet(Set<String> reminderSet) {
prefs_edit.putStringSet("reminderSet", reminderSet);
clear();
prefs_edit.commit();
}
public Set<String> getReminderSet() {
return prefs.getStringSet("reminderSet", null);
}
public void clear() {
prefs_edit.clear();
prefs_edit.commit();
}
}
分别使用SetReminderSet(reminderSet)
和getReminderSet()
方法设置或获取值。
<强> MainActivity:强>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private DemoPrefs prefs;
private EditText editText;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
setListener();
}
private void initialize() {
prefs = DemoPrefs.getInstance(this);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
}
private void setListener() {
button.setOnClickListener(this);
}
public void storeReminder(String reminderString) {
Set<String> reminderSet = null;
if (prefs.getReminderSet() == null)
reminderSet = new HashSet<String>();
else
reminderSet = prefs.getReminderSet();
reminderSet.add(reminderString);
prefs.setReminderSet(reminderSet);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
if (prefs.getReminderSet() != null)
System.out.println("OLD SIZE: " + prefs.getReminderSet().size());
storeReminder("Set: " + editText.getText().toString().trim() + "");
if (prefs.getReminderSet() != null)
System.out.println("NEW SIZE: " + prefs.getReminderSet().size());
} else {
Toast.makeText(this, "Please enter data", Toast.LENGTH_SHORT).show();
}
editText.setText("");
break;
}
}
}
<强> activity_main.xml中:强>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pairroxz.demoapp.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/button"
app:layout_constraintTop_toTopOf="parent"
tools:hint="@string/edit_message" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="@string/button_send"
app:layout_constraintBaseline_toBaselineOf="@+id/editText"
app:layout_constraintLeft_toRightOf="@+id/editText"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
答案 2 :(得分:0)
好的,我发现了原因:
您必须删除存储在首选项中的字符串集,并在其位置添加新副本。
我在这篇文章的答案中找到了它:
Android: String set preference is not persistent
我改变了我的存储代码,它工作正常:
public static void storeReminder (Context context, String reminderID, String reminderString){
// Get the set of reminder strings
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Set <String> remindersStringSet = sharedPreferences.getStringSet(context.getResources().getString(R.string.reminders_hashset_key), new HashSet<String>());
// Add the new reminder string to the reminder string set
remindersStringSet.add(reminderString);
// Get the shared preferences editor
SharedPreferences.Editor editor = sharedPreferences.edit();
// Delete the current set in shared preferences
editor.remove(context.getResources().getString(R.string.reminders_hashset_key));
editor.apply();
// Save the NEW version of reminder string set
editor.putStringSet(context.getResources().getString(R.string.reminders_hashset_key), new HashSet<String>(remindersStringSet));
editor.apply();
}
感谢大家的帮助