如何使RealmList可以生成

时间:2017-04-25 19:56:04

标签: java android realm

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

这是我的代码:

        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "hotels";
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $stmt = $conn->prepare("INSERT INTO users (username, email, password, number) 
        VALUES (:username, :email, :password, :number)");
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':password', $password);
        $stmt->bindParam(':number', $number);
        $username = $_POST['textfield'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $number = $_POST['number'];
        $stmt->execute();

pd:SomeOtherModel类还实现了Parcelable接口并扩展了RealmObject

3 个答案:

答案 0 :(得分:21)

实际上你可以

首先,我们讨论的是未经管理的实体。

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

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];
        }
    };
}

我希望这可能有所帮助,&#39;。

答案 1 :(得分:2)

您将无法轻松制作RealmList parcelable,您应该将其转换为JSON并将其作为字符串包裹,或者将Parceler库与this gist一起使用。我认为使用自定义适配器的JSON实际上更可靠。

使用Kotlin

编辑,您可以关注this approach

答案 2 :(得分:0)

我认为不可能使RealmObject成为可能的。 但是,如果我理解你的情况,你可以使用copyFromRealm()方法从Realm中分离对象,并用它做任何你想做的事。