简体   繁体   English

SharedPreferences不起作用

[英]SharedPreferences don't work

i'm having little problems using the SharedPreferences. 我在使用SharedPreferences时遇到了一些问题。 I want to save and later load strings to/from the Preferences. 我要保存字符串,然后再将其加载到首选项中。 I initialize my prefs in the onCreate-method: 我在onCreate方法中初始化我的偏好:

prefs = this.getSharedPreferences("com.example.android_test", Activity.MODE_PRIVATE);

I do saving in another method: 我确实用另一种方法保存:

    public void saveUser()
    {   
        prefs.edit().putString("username", username);
        prefs.edit().putString("password", password);
        prefs.edit().apply();
    }

And loading in yet another method: 并加载另一种方法:

    public void loadUser()
    {
        username = prefs.getString("username", "default");
        password = prefs.getString("password", "test");
    }

And those are my Test-Methods: 这些是我的测试方法:

    public void showUser(View v) 
    {
       loadUser();
       text.setText(username);
    }

    public void addUser(View v)
    {
        changeUser(eingabe.getText().toString(),"newpass");
    }

    public void changeUser(String user, String pass)
    {
        username = user;
        password = pass;
        saveUser();
    }

username and password are global, private Strings, eingabe is an EditText and text is a TextView. 用户名和密码是全局的私有字符串,eingabe是EditText,文本是TextView。 However, when executing showUser() i only get the defaultvalue displayed to the TextView, even if im using saveUser with different usernames... No crash or anything... its just only the defaultvalue being shown... 但是,当执行showUser()时,即使我使用具有不同用户名的saveUser,我也只能将默认值显示在TextView上。

您忘记了进行编辑:

prefs.edit().putString("username", username).commit();
 prefs.edit().putString("username", username);
 prefs.edit().putString("password", password);

Change this each time edit will return diffrent instance so your changes wont get committed. 每次edit都会返回不同的实例,请更改此设置,这样您的更改就不会提交。

Editor editor = prefs.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();

Try this. 尝试这个。

Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. 与commit()将其首选项同步写到持久性存储中的方式不同,apply()立即将其更改提交到内存中的SharedPreferences,但是将异步提交到磁盘,并且不会收到任何故障通知。 If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself. 如果此SharedPreferences上的另一个编辑器在apply()仍未完成的情况下执行常规commit(),则commit()将阻塞,直到所有异步提交以及提交本身都完成为止。

Ok, apply is same as commit , it only writes the preferences atomically making it thread safe. 好的,apply与commit相同 ,它仅原子地写入优先级,使其线程安全。 (It requires API Level 9 though, so be careful) (尽管它需要API级别9,所以要小心)

See the documentation 请参阅说明文件

The problem is what Rajesh CP is saying. 问题是Rajesh CP所说的。 You need to create an editor pointer and then apply: 您需要创建一个编辑器指针,然后应用:

Editor editor = prefs.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.apply();

Else, every time you create a new instance of the editor and when you apply the changes, the editor that is being instructed to apply the changes, doesn't have any changes 否则,每次创建编辑器的新实例时,当您应用更改时,被指示要应用更改的编辑器都没有任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM