1

以下是代码。

将对象存储到共享首选项中

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 对象。

4

2 回答 2

2

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

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

或者

覆盖 AccountVO 类中的 toString 方法以返回一个字符串,稍后您可以在稍后从 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]);
}
于 2013-01-02T19:31:04.133 回答
2

这是将对象保存到SharedPreferences. 谷歌的 GSON 很棒。

使用GSON将您的对象转换为JSON字符串,然后将其保存到: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);
}
于 2013-01-02T19:57:26.953 回答