MaaAn13
MaaAn13

Reputation: 268

How to parcel RealmList<String>?

I've a list of strings that I want to parcelize and then put into Realm.

public class X extends RealmObject ... 

private @SerialName("ha") RealmList<String> list;

public void writeToParcel(Parcel out, int i) {
    //how to do this?  
}

private X(Parcel in) {
    ///how to do this?  
}

I'm not sure how can I writeToParcel and readToParcel RealmList?

Upvotes: 0

Views: 557

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81588

You can turn any collection into Parcelable by writing out its size and then re-constructing it.

private @SerialName("ha") RealmList<String> list;

public void writeToParcel(Parcel out, int i) {
    out.writeInt(list != null ? 1 : 0);
    if(list != null) {
        out.writeInt(list.size());
        for(String item: list) {
            out.writeString(item);
        }
    }
}

private X(Parcel in) {
    boolean hasList = in.readInt() > 0;
    if(hasList) {
        int size = in.readInt();
        list = new RealmList<String>(size);
        for(int i = 0; i < size; i++) {
            list.add(in.readString());
        }
    }
}

Upvotes: 1

Related Questions