数据无法保存到共享首选项

时间:2018-02-22 03:20:32

标签: java android

Layout

嗨!所以我一直在尝试制作一个基本的笔记应用程序而且我遇到了一堵墙。我花了好几个小时试图将我的数据保存到我的sharedPreferences,但无论我尝试什么它似乎都不起作用。我已经将日志添加到应用程序中,以便我们可以检查发生了什么。

日志:

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

(当我点击添加注释按钮时)

02-22 18:29:54.500 4929-4929 / com.example.jackson.collegeplanner I / TEST:newNote添加到notesSet

代码:

public class Schedule extends AppCompatActivity {

    ArrayList<String> notes = new ArrayList<>();

    //        SharedPreferences sharedPreferences = this .getSharedPreferences("com.example.jackson.collegeplanner", Context.MODE_PRIVATE);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schedule);
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, notes);
        ListView listView = (ListView) findViewById(R.id.listView);
        SharedPreferences myPref = this.getSharedPreferences("com.example.jackson.collegeplanner", Context.MODE_PRIVATE);

        Set<String> notesSet = myPref.getStringSet("NN", null);

        if(notesSet != null){
            notes.addAll(notesSet);
            listView.setAdapter(arrayAdapter);
            Log.i("TEST", "notesSet didn't return null!");
        }
        else{
            notesSet = new HashSet<String>();
            notesSet.add("Ya note's set is empty");
            notes.addAll(notesSet);
            listView.setAdapter(arrayAdapter);
            Log.i("TEST", "noteSet returned null");
        }

        myPref.edit().putStringSet("NN", notesSet).apply();

    }

    public void AddNote(View view){
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, notes);
        ListView listView = (ListView) findViewById(R.id.listView);
        EditText editText = (EditText) findViewById(R.id.editText);
        String newNote = editText.getText().toString();
        SharedPreferences myPref = this.getSharedPreferences("com.example.jackson.collegeplanner", Context.MODE_PRIVATE);
        Set<String> notesSet = myPref.getStringSet("NN", null);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        notesSet.add(newNote);
        Log.i("TEST", "newNote added to notesSet");
        notes.clear();
        notes.addAll(notesSet);

        editText.setText("");

        myPref.edit().putStringSet("NN", notesSet).apply();

        listView.setAdapter(arrayAdapter);
        }


    }

3 个答案:

答案 0 :(得分:0)

您可以使用

 if(myPref.edit().putStringSet("notes", set).commit())
    Log.d(TAG , "SAVED");
 else 
    Log.d(TAG , "Not Saved");

检查是否实际保存了共享首选项。

答案 1 :(得分:0)

代码更改

 `
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();
}
`

答案 2 :(得分:0)

答案来自我制作的重复帖子,并在那里得到答复。

哦,我只记得你在SharedPreferences中遇到的字符串集,你可能会遇到这种情况。 (我承认,当我这样做时,我并没有非常彻底地测试你的代码。)你不能尝试修改你从getStringSet()获得的Set,然后将其保存回来。您需要实例化一个新的,并将其传递给putStringSet()。在您的代码中,一个简单的修复方法是:Set<String> notesSet = new HashSet<String>(myPref.getStringSet("NN", null)); - Mike M。