Android 尝试使用首选项来保存数据
我试图通过编写一个简短的程序来将 ted
存储在关键 board
中,然后加载 中的值来了解
。 当我使用 getString 加载值时,boardstr 的值不会改变。Preferences
的工作原理>boardstr
boardstr= new String();
boardstr="fred";
// set the prefrence to ted
this.getPreferences(MODE_PRIVATE).edit().putString("board","ted");
// kload the prfrence in boardstr
this.getPreferences(MODE_PRIVATE).getString("board",boardstr);
// boardstr stil equals fred, not ted
I’m trying to learn how Preferences
work by writing a short program to store ted
in the key board
and then load the value in boardstr
.
When I use getString
to load the value, the value of boardstr
does not change.
boardstr= new String();
boardstr="fred";
// set the prefrence to ted
this.getPreferences(MODE_PRIVATE).edit().putString("board","ted");
// kload the prfrence in boardstr
this.getPreferences(MODE_PRIVATE).getString("board",boardstr);
// boardstr stil equals fred, not ted
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
commit()< /code>
或
apply()
更改以便保存并生效。例如:
You need to either
commit()
orapply()
the changes in order for them to be saved and take effect.E.g.: