Android的 - 保存对象共享preferences和应用程序得到它的任何地方 [英] Android - save Object to SharedPreferences and get it anywhere in the app

查看:265
本文介绍了Android的 - 保存对象共享preferences和应用程序得到它的任何地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有一个自定义其中包含了一些常规数据(名称等)用户类。我需要保存该对象,并得到它在应用程序中的其他网页,随时随地。我做了一个辅助类公共final类GeneralMethods 与我使用(当然静态)很多很多的方法。结果为了使用 GSON 库来保存数据Im。我做了这个方法:结果

 公共静态无效SAVEDATA(上下文CON,字符串变量,字符串数据)
{
    共享preferences preFS = preferenceManager.getDefaultShared preferences(CON);
    。prefs.edit()putString(可变数据)。适用();
}

要保存对象,我用这个方法如下:结果

  GSON GSON =新GSON();
字符串stringUser = gson.toJson(NEWUSER);
GeneralMethods.saveData(VerificationActivity.this,userObject,stringUser);

要加载数据后面,我使用这个静态方法:结果

 公共静态字符串的getData(上下文CON,字符串变量,字符串设置defaultValue)
{
    共享preferences preFS = preferenceManager.getDefaultShared preferences(CON);
    字符串数据= prefs.getString(变量,设置defaultValue);
    返回的数据;
}

我真的不知道如何取回数据,这是我迄今所做的:

  GSON GSON =新GSON();
字符串用户=;
字符串值=;
用户= GeneralMethods.getData(SplashScreenActivity.this,价值userObject);

林与的getData 方法,我该如何解析来自字符串回<$ c中的数据挣扎$ C>用户键入<?/ p>

结果编辑结果
我试过的建议波纹管,我总是得到 NULL 。也许我不保存对象以正确的方式?

结果EDIT2结果
看来我不是正确生成的对象,因此没有被保存。这是用户单身类:结果

 公共类用户实现Serializable {    私有静态用户userInstance = NULL; //类的唯一实例
    私人静态字符串用户名; // = userName的短电话号码
    私人用户(){}    公共静态用户的getInstance(){
        如果(userInstance == NULL){
            userInstance =新用户();
        }
        返回userInstance;
    }    公共静态用户getUserInstance(){
        返回userInstance;
    }    公共字符串getUserName(){
        返回this.userName;
    }    公共静态无效setUserName(用户名字符串){
        User.userName =用户名;
    }    公共静态无效的init(字符串_username){
        User.setUserName(_username);
    }
}

这是我怎么设置相关数据的对象(用户名作为构造函数的参数):

  User.init(名);

这是我怎么的对象转换为字符串

  GSON GSON =新GSON();
    串stringUser = gson.toJson(User.getInstance());
GeneralMethods.saveData(VerificationActivity.this,userObject,stringUser);


解决方案

替换为以下

现有的用户类别

 公共类用户实现Serializable
{    私有静态用户userInstance = NULL; //类的唯一实例
    私人字符串用户名; // = userName的短电话号码
    私人用户(){}
    公共静态用户的getInstance()
    {
        如果(userInstance == NULL)
        {
            userInstance =新用户();
        }
        返回userInstance;
    }    公共字符串getUserName()
    {
        返回用户名;
    }    公共无效setUserName(字符串p_userName)
    {
        的userName = p_userName;
    }    @覆盖
    公共字符串的toString()
    {
        返回用户[用户名=+ getUserName()+];
    }
}

初​​始化用户名

 用户m_user = User.getInstance();
m_user.setUserName(名);

将对象转换为字符串

  GSON GSON =新GSON();
字符串stringUser = gson.toJson(m_user);
GeneralMethods.saveData(VerificationActivity.this,userObject,stringUser);

In my app I have a custom User class which holds some regular data (name etc...). I need to save that object and get it anywhere and anytime in other pages of the app. I made a helper class public final class GeneralMethods with many methods which I use a lot (static, of course).
In order to save the data Im using Gson library. I made this method:

public static void saveData(Context con, String variable, String data)
{
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);
    prefs.edit().putString(variable, data).apply();
}

To save an object, I use this method as follows:

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

To load the data back, I'm using this static method:

public static String getData(Context con, String variable, String defaultValue)
{
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(con);
    String data = prefs.getString(variable, defaultValue);
    return data;
}

I dont really know how to get the data back, this is what I've done so far:

Gson gson = new Gson();
String user="";
String value="";
user = GeneralMethods.getData(SplashScreenActivity.this,value,"userObject");

Im struggling with the getData method, how do I parse the data from String back to the User type?


EDIT
I tried the suggestions bellow and I always get NULL. Maybe I dont save the object in the right way?


EDIT2
It seems Im not generating the object correctly and therefore nothing is being saved. This is the user "Singleton" class:

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

This is how i setup the object with the relevant data (user name as the constructor parameter):

   User.init(name);

This is how i convert the object to a String:

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

解决方案

Replace your existing User class with below

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() + "]";
    }
}

Initialize User Name

User m_user = User.getInstance();
m_user.setUserName(name);

Convert the object to a String

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

这篇关于Android的 - 保存对象共享preferences和应用程序得到它的任何地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆