如何使RealmList成为可能的 [英] How to make a RealmList parcelable

查看:139
本文介绍了如何使RealmList成为可能的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Realm模型中实现Parcelable接口,但我不知道如何在Parcel中编写RealmList

i need to implement the Parcelable interface in a Realm model but i have no idea how to write a RealmList in a Parcel

这里是我的代码:

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类还实现了Parcelable接口并扩展了RealmObject

pd: the SomeOtherModel class also implements the Parcelable interface and extends RealmObject

推荐答案

实际上你可以

首先我们讨论的是非托管实体。

Firstly we are talking about un-managed entities.

然后你需要用 new RealmList<>()初始化realmList。然后使用 addAll 方法将数据添加到集合中。就是这样!
以下是一个示例:

Then you need to initialize realmList with new RealmList<>() . Then add data to it as collection with addAll method.That's it! Here is a sample:

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 that may help,'.

这篇关于如何使RealmList成为可能的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆