以下是代码。
将对象存储到共享首选项中
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 对象。