2016-11-16 16 views
0

프로젝트에서 직렬화를 위해 Parceler 라이브러리를 사용하고 있습니다. Parceler 라이브러리를 사용하여 영역 객체 직렬화

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

    public int id; 
    public int title; 
} 

내가하는 A 객체 직렬화와 같은 의도에 넣어 :

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); 

그리고이 같은 직렬화 :

나는 이런 RealmObject 클래스가

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