2016-11-16 4 views
0

私はプロジェクトでシリアル化のためにパーセルライブラリを使用しています。パーセルライブラリを使用してrealmオブジェクトをシリアライズ

私はこのようなRealmObjectクラスを持っている:

@Parcel(implementations = {ARealmProxy.class}, value = Parcel.Serialization.BEAN, analyze = {A.class}) 
class A extends RealmObject { 

    public int id; 
    public int title; 
} 

私はオブジェクトをシリアル化し、このよう意図にそれを置く:

Intent intent = new Intent(context, Main); 
Bundle bundle = new Bundle(); 
A a = new A(); 
a.id = 10; 
a.title = "title"; 
bundle.putParcelable("mykey", Parcels.wrap(a)) 
intent.putExtras(bundle); 
context.startActivity(intent); 

そして、私はこのようにそれをデシリアライズ:

Bundle bundle = getIntent().getExtras(); 
A a = Parcels.unwrap(bundle.getParcelable("mykey")); 
// a's properties are null 

aのプロパティはnullです。 どうすれば修正できますか?

答えて

0

getters/settersを使用する必要があります。

@Parcel(implementations = {ARealmProxy.class}, 
     value = Parcel.Serialization.BEAN, 
     analyze = {A.class}) 
class A extends RealmObject { 
    @PrimaryKey 
    private int id; 

    private int title; 

    public int getId() { return id; } 
    public void setId(int id) { this.id = id; } 
    public int getTitle() { return title; } 
    public void setTitle(int title) { this.title = title; } 
} 

技術的に、あなたはRealmObjectからParcelableオブジェクトを作成すべきではありませんが。インテントバンドルからプライマリキーを送信し、他のアクティビティでオブジェクトを再クエリする必要があります。

Intent intent = new Intent(context, Main.class); 
Bundle bundle = new Bundle(); 
bundle.putLong("id", 10); 

そして

Bundle bundle = getIntent().getExtras(); 
A a = realm.where(A.class).equalTo(AFields.ID, bundle.getLong("id")).findFirst(); 
+0

私はちょうど意思にIDを送信することはできません。それはサーバーから来て、それを完全に送信する必要があるからです。 – Hojjat

+0

なぜそれを領域に残さないのですか? – EpicPandaForce

関連する問題