java - Android - 将对象保存到 SharedPreferences 并在应用程序中的任何位置获取它

标签 java android sharedpreferences gson

在我的应用程序中,我有一个自定义的 User 类,它包含一些常规数据(名称等...)。我需要保存该对象并在应用程序的其他页面中随时随地获取它。我制作了一个辅助类 public final class 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。也许我没有以正确的方式保存对象?


编辑2
似乎我没有正确生成对象,因此没有保存任何内容。这是用户“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);

最佳答案

将您现有的用户类替换为以下

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);

将对象转换为字符串

Gson gson = new Gson();
String stringUser = gson.toJson(m_user);
GeneralMethods.saveData(VerificationActivity.this,"userObject",stringUser);

关于java - Android - 将对象保存到 SharedPreferences 并在应用程序中的任何位置获取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29406945/

相关文章:

java - Android:转换为 Dalvik 格式失败:无法执行 dex:null

android - 静态共享首选项

Java: token "else"上的语法错误

java - 如何在 Ruby 中访问上传的文件

java - CAMT.053 Java 解析器

android - 谷歌 Play 商店中的平板电脑未显示 Phonegap 应用程序

java - 如何在 Medium 或 Telegram X 应用程序中实现 StatusBar 效果?

java - Android - 无法使用 R.string 访问字符串

java - 如何在另一个 Activity 中检索共享首选项的值?

android - 检查 SharedPrefecences 是否存在于 Android