Java 赢得的数据';无法保存到共享首选项

Java 赢得的数据';无法保存到共享首选项,java,android,Java,Android,嗨!所以我一直在尝试制作一个基本的便笺应用程序,但我遇到了麻烦。我花了好几个小时试图将数据保存到我的SharedReferences,但不管我怎么做,它似乎都不起作用。我已经在应用程序中添加了日志,以便我们可以检查正在发生的事情 日志: 02-22 18:27:56.767 4929-4929/com.example.jackson.collegeplanner I/TEST:notesSet未返回null (当我单击addnote按钮时) 02-22 18:29:54.500 4929-492

嗨!所以我一直在尝试制作一个基本的便笺应用程序,但我遇到了麻烦。我花了好几个小时试图将数据保存到我的SharedReferences,但不管我怎么做,它似乎都不起作用。我已经在应用程序中添加了日志,以便我们可以检查正在发生的事情

日志:

02-22 18:27:56.767 4929-4929/com.example.jackson.collegeplanner I/TEST:notesSet未返回null

(当我单击addnote按钮时)

02-22 18:29:54.500 4929-4929/com.example.jackson.collegeplanner I/测试:新注释添加到注释集

代码:

公共类时间表扩展了AppCompative活动{
ArrayList notes=新的ArrayList();
//SharedReferences SharedReferences=this.getSharedReferences(“com.example.jackson.collegeplanner”,Context.MODE\u PRIVATE);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,注释);
ListView ListView=(ListView)findViewById(R.id.ListView);
SharedReferences myPref=this.getSharedReferences(“com.example.jackson.collegeplanner”,Context.MODE\u PRIVATE);
Set notesSet=myPref.getStringSet(“NN”,null);
如果(notesSet!=null){
注:添加全部(注集);
setAdapter(arrayAdapter);
Log.i(“TEST”,“notesSet没有返回null!”);
}
否则{
notesSet=新的HashSet();
notesSet.add(“Ya note的集合为空”);
注:添加全部(注集);
setAdapter(arrayAdapter);
Log.i(“TEST”,“noteSet返回null”);
}
myPref.edit().putStringSet(“NN”,notesSet).apply();
}
公共void AddNote(视图){
ArrayAdapter ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,注释);
ListView ListView=(ListView)findViewById(R.id.ListView);
EditText EditText=(EditText)findViewById(R.id.EditText);
字符串newNote=editText.getText().toString();
SharedReferences myPref=this.getSharedReferences(“com.example.jackson.collegeplanner”,Context.MODE\u PRIVATE);
Set notesSet=myPref.getStringSet(“NN”,null);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
注释集。添加(新注释);
Log.i(“测试”,“添加到注释集的新注释”);
notes.clear();
注:添加全部(注集);
editText.setText(“”);
myPref.edit().putStringSet(“NN”,notesSet).apply();
setAdapter(arrayAdapter);
}
}
您可以使用

 if(myPref.edit().putStringSet("notes", set).commit())
    Log.d(TAG , "SAVED");
 else 
    Log.d(TAG , "Not Saved");
检查共享首选项是否已实际保存。

在您的代码更改中

 `
if(set == null){
    set = new HashSet<String>();
    notes.add("Initial Notes");
    set.addAll(notes);
}
`
`
if(set==null){
set=新的HashSet();
注释。添加(“初始注释”);
set.addAll(注释);
}
`

`
if(set==null){
set=新的HashSet();
注释。添加(“初始注释”);
set.addAll(注释);
myPref.edit().putStringSet(set.apply();
}
`

答案来自我写的一篇重复的帖子,在那里得到了回答


哦,我刚刚想起了一个在SharedReferences中使用字符串集的陷阱,你可能会遇到它。(我承认我在测试代码时没有真正彻底地测试代码。)您不能尝试修改从
getStringSet()
获得的集,然后将其保存回去。您需要实例化一个新的,并将其传递给
putStringSet()
。在您的代码中,一个简单的修复是:
setnotesset=newhashset(myPref.getStringSet(“NN”,null))–迈克·M.

那么,你遇到了什么问题?这张图片显示了真实的结果。我正在努力确保我的数据在我点击加号后保存。出于某种原因,当我退出应用程序、杀死它并重新打开它时,它不会保存。所以当它重新加载时,我只会得到这两个项目。你插入的第二个项目,对吗?我没有通过editText插入它。在旧代码中,(我已经试着让它工作了几个小时)我通过编程将行插入到代码中,但我无法通过+保存任何内容并在下一次onCreate中加载它。
 `
if(set == null){
    set = new HashSet<String>();
    notes.add("Initial Notes");
    set.addAll(notes);
}
`
`
if(set == null){
    set = new HashSet<String>();
    notes.add("Initial Notes");
    set.addAll(notes);
    myPref.edit().putStringSet(set).apply();
}
`