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。Google の 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 に答える