java – Android – 将对象保存到SharedPreferences并在应用程序中的任何位置获取它
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Android – 将对象保存到SharedPreferences并在应用程序中的任何位置获取它,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2832字,纯文字阅读大概需要5分钟。
内容图文
![java – Android – 将对象保存到SharedPreferences并在应用程序中的任何位置获取它](/upload/InfoBanner/zyjiaocheng/761/1c4f315202a845759cba7f2419e2d1d2.jpg)
在我的应用程序中,我有一个自定义User类,它包含一些常规数据(名称等…).我需要保存该对象并随时随地在应用程序的其他页面中获取它.我创建了一个帮助类公共最终类GeneralMethods,我使用了很多方法(当然是静态的).为了使用Gson库保存数据.我做了这个方法:
public static void saveData(Context con, String variable, String data)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);
prefs.edit().putString(variable, data).apply();
}
要保存对象,我使用此方法如下:
Gson gson = new Gson();
String stringUser = gson.toJson(newUser);
GeneralMethods.saveData(VerificationActivity.this,"userObject",stringUser);
要重新加载数据,我正在使用这个静态方法:
public static String getData(Context con, String variable, String defaultValue)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);
String data = prefs.getString(variable, defaultValue);
return data;
}
我真的不知道如何获取数据,这是我到目前为止所做的:
Gson gson = new Gson();
String user="";
String value="";
user = GeneralMethods.getData(SplashScreenActivity.this,value,"userObject");
我正在努力解决getData方法,如何将String中的数据解析回User类型?
编辑
我尝试了下面的建议,我总是得到NULL.也许我不以正确的方式保存对象?
EDIT2
我似乎没有正确生成对象,因此没有任何东西被保存.这是用户“Singleton”类:
public class User implements Serializable {
private static User userInstance=null; //the only instance of the class
private static String userName; //userName = the short phone number
private User(){}
public static User getInstance(){
if(userInstance ==null){
userInstance = new User();
}
return userInstance;
}
public static User getUserInstance() {
return userInstance;
}
public String getUserName(){
return this.userName;
}
public static void setUserName(String userName) {
User.userName = userName;
}
public static void init(String _userName) {
User.setUserName(_userName);
}
}
这是我如何使用相关数据(用户名作为构造函数参数)设置对象:
User.init(name);
这是我如何将对象转换为String:
Gson gson = new Gson();
String stringUser = gson.toJson(User.getInstance());
GeneralMethods.saveData(VerificationActivity.this,"userObject",stringUser);
解决方法:
用以下替换现有的User类
public class User implements Serializable
{
private static User userInstance = null; // the only instance of the class
private String userName; // userName = the short phone number
private User(){}
public static User getInstance()
{
if (userInstance == null)
{
userInstance = new User();
}
return userInstance;
}
public String getUserName()
{
return userName;
}
public void setUserName(String p_userName)
{
userName = p_userName;
}
@Override
public String toString()
{
return "User [userName=" + getUserName() + "]";
}
}
初始化用户名
User m_user = User.getInstance();
m_user.setUserName(name);
将对象转换为String
Gson gson = new Gson();
String stringUser = gson.toJson(m_user);
GeneralMethods.saveData(VerificationActivity.this,"userObject",stringUser);
内容总结
以上是互联网集市为您收集整理的java – Android – 将对象保存到SharedPreferences并在应用程序中的任何位置获取它全部内容,希望文章能够帮你解决java – Android – 将对象保存到SharedPreferences并在应用程序中的任何位置获取它所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。