开发者

Load sharedpreferences not giving me a value

开发者 https://www.devze.com 2023-03-29 22:03 出处:网络
Here is my code. SharedPreferences sharedPreferences = getSharedPreferences(values, MODE_PRIVATE); str1开发者_如何学Python = sharedPreferences.getString(\"lu121\", \"test\");

Here is my code.

        SharedPreferences sharedPreferences = getSharedPreferences(values,
            MODE_PRIVATE);
    str1开发者_如何学Python = sharedPreferences.getString("lu121", "test");
    str2 = sharedPreferences.getString("lp5151", "test");
    et_username.setText(str1);
    et_pass.setText(str2);

by default str1 and str2 should have values of test but when i open the android application the edittexts are not set as test instead i see a blank. Is there something wrong with my code?


You need to call commit() to save the values (you didn't include any code where you are writing.

...
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("lu", "testlu");
editor.putString("lp", "testlp");
editor.commit();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号