How to make RealmList available

I need to implement the Parcelable interface in a Realm model, but I don't know how to write a RealmList in Parcel

here is my code:

public class SomeModel extends RealmObject implements Parcelable { public long id; public RealmList<SomeOtherModel> otherModels; protected SomeModel(Parcel in) { id = in.readLong(); //here i need to otherModel RealmList from parcel } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeLong(id); // here i need to write the otherModels RealmList } //other methods omitted } 

pd: SomeOtherModel also implements the Parcelable interface and extends RealmObject

+5
source share
3 answers

In fact you can

Firstly, we are talking about unmanaged entities.

Then you need to initialize realmList with new RealmList<>() . Then add data to it as a collection using the addAll method. It! Here is an example:

Person.java:

 package com.entity; import android.os.Parcel; import android.os.Parcelable; import java.util.ArrayList; import io.realm.RealmList; /** * Person * Created by Maher Abuthraa on 6/9/17. */ public class Person implements Parcelable { @PrimaryKey private long id; private String name; private RealmList<Dog> mRealmList; public Person() { } public Person(long id, String name) { this.id = id; this.name = name; } public long getId() { return id; } public Person setId(long id) { this.id = id; return this; } public String getName() { return name; } public Person setName(String name) { this.name = name; return this; } public RealmList<Dog> getRealmList() { return mRealmList; } public Person setRealmList(ArrayList<Dog> dogList) { mRealmList = new RealmList<>(); mRealmList.addAll(dogList); return this; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeLong(this.id); dest.writeString(this.name); dest.writeTypedList(this.mRealmList); } protected Person(Parcel in) { this.id = in.readLong(); this.name = in.readString(); this.mRealmList = new RealmList<>(); this.mRealmList.addAll(in.createTypedArrayList(Dog.CREATOR)); } public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() { @Override public Person createFromParcel(Parcel source) { return new Person(source); } @Override public Person[] newArray(int size) { return new Person[size]; } }; } 

Dog.java:

 package com.entity; import android.os.Parcel; import android.os.Parcelable; import io.realm.RealmObject; import io.realm.annotations.PrimaryKey; /** * Dog * Created by Maher Abuthraa on 6/9/17. */ public class Dog extends RealmObject implements Parcelable { @PrimaryKey private long id; private String name; private int age; public Dog() { } public Dog(long id, String name, int age) { this.id = id; this.name = name; this.age = age; } public long getId() { return id; } public Dog setId(long id) { this.id = id; return this; } public String getName() { return name; } public Dog setName(String name) { this.name = name; return this; } public int getAge() { return age; } public Dog setAge(int age) { this.age = age; return this; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeLong(this.id); dest.writeString(this.name); dest.writeInt(this.age); } protected Dog(Parcel in) { this.id = in.readLong(); this.name = in.readString(); this.age = in.readInt(); } public static final Parcelable.Creator<Dog> CREATOR = new Parcelable.Creator<Dog>() { @Override public Dog createFromParcel(Parcel source) { return new Dog(source); } @Override public Dog[] newArray(int size) { return new Dog[size]; } }; } 

I hope this can help. "

+18
source

I think it's impossible to make RealmObject weighty. But if I understand your case correctly, you can use the copyFromRealm () method to disconnect the object from Realm and do whatever you want with it.

0
source

You cannot easily make RealmList verbatim; you must either convert it to JSON, or put it as a string, or use the Parceler library with that meaning . I think that JSON with your custom adapter is actually more reliable.

0
source

Source: https://habr.com/ru/post/1267156/


All Articles