保存并加载对象Android

时间:2013-01-02 19:22:43

标签: android sharedpreferences

以下是代码。

将对象存储到共享首选项

public void saveObject()
    {
        AccountVO accountVO1 = new AccountVO("1", "scott", "password", "scott@mail.com", "Scott", "Stanford");

        accountsSharedPreference = context.getSharedPreferences("AccountsPrefsFile",Context.MODE_WORLD_PRIVATE);
        accountsPreferenceEdit = accountsSharedPreference.edit();

        accountsPreferenceEdit.putString("accountObject", accountVO1.toString());
        accountsPreferenceEdit.commit();
}

从同一共享首选项中检索对象

public AccountVO loadObject()
    {
        AccountVO accountVO1 = null;

        accountsSharedPreference = context.getSharedPreferences("AccountsPrefsFile",context.MODE_PRIVATE);

        String accounntsObjectString = accountsSharedPreference.getString("accountObject", "");

        //My issue ? --- How to retreive the AccountVO object from "accountObject" String here????

        return accountVO1;
}

我的AccountVO类代码如下:

    public class AccountVO
    {

        private String id;
        private String username;
        private String password;
        private String email;
        private String firstName;
        private String lastName;

public AccountVO(String tempID, String tempUserName, String tempPassword, String tempEmail, String tempFirstName, String tempLastName)
    {
            this.id = tempID;
            this.username = tempUserName;
            this.password = tempPassword;
            this.email = tempEmail;
            this.firstName = tempFirstName;
            this.lastName = tempLastName;   
        }
        public String getId()
        {
            return id;
        }
        public String getUserName()
        {
            return username;
        }
        public String getEmail()
        {
            return email;
        }
        public String getFirstName()
        {
            return firstName;
        }
        public String getLastName()
        {
            return lastName;
        }
    }

任何人都告诉我如何从loadObject方法中的accountObject字符串中检索AccountVO对象。

2 个答案:

答案 0 :(得分:2)

您无法使用toString()属性构造对象。 Object.toString()仅返回对象的哈希值表示,而不是内存地址或其他任何内容。这意味着多个对象可以从toString()返回相同的结果。例如: new Byte(10).toString()"10".toString()

要将对象存储在SharedPreferences中,我建议您将它们存储为键值对,因为您知道AccountVO对象的确切格式。 (在该类中添加帮助程序可能会为您返回一个键值对映射)

重写AccountVO类中的toString方法,以返回一个String,稍后当您从SharedPreferences中检索时,可以轻松解密。像这样:

@Override
public String toString() {
  return id+"<delim>"+username+"<delim>"+password+"<delim>"+email+"<delim>"+firstName+"<delim>"+lastName;
}

然后在loadObject()方法中,使用:

重建对象
public AccountVO loadObject()
{
    AccountVO accountVO1 = null;

    accountsSharedPreference = context.getSharedPreferences("AccountsPrefsFile",context.MODE_PRIVATE);

    String accounntsObjectString = accountsSharedPreference.getString("accountObject", "");

    String[] parts = accounntsObjectString.split("<delim>");

    return new accountVO1(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]);
}

答案 1 :(得分:2)

这是一种将对象保存到SharedPreferences的更简单明了的方法。谷歌的GSON非常棒。

使用JSON将您的对象转换为GSON字符串,然后将其保存到SharedPreferences

AccountVO accountVO1 = new AccountVO("1", "scott", "password", "scott@mail.com", "Scott", "Stanford");

accountsSharedPreference = context.getSharedPreferences("AccountsPrefsFile",Context.MODE_WORLD_PRIVATE);
accountsPreferenceEdit = accountsSharedPreference.edit();

Gson gson = new Gson();
String objStr = gson.toJson(accountV01);

accountsPreferenceEdit.putString("accountObject", objStr);
accountsPreferenceEdit.commit();

要检索已保存的对象,请执行以下操作:

Gson gson = new Gson();
accountsSharedPreference = context.getSharedPreferences("AccountsPrefsFile",Context.MODE_WORLD_PRIVATE);
String savedObjStr = accountsSharedPreference.getString("accountObject", null);
if(savedObjStr != null) {
    AccountVO accountVO1 = gson.fromJson(savedObjStr, AccountVO.class);
}