[英]How do I properly implement a Realm List into my custom object?
So I'm trying to implement a Realm List into my class, however, the documentation that I can find at Realm.io doesn't clarify whether it needs a getter/setter and if it does what it should look like. 因此,我试图在我的类中实现一个领域列表,但是,我在Realm.io上找到的文档并没有阐明它是否需要吸气剂/ 塞特剂,以及它是否应执行其外观。 So here's my code:
所以这是我的代码:
public class Novel extends RealmObject {
@PrimaryKey
private static int id;
@Required
private static String name;
//Establish relationship with Novel (single) and Chapter (many)
private RealmList<Chapter> chapters;
public static int getId() {
return id;
}//end getId
public static void setId(int id) {
Novel.id = id;
}//end setId
public static String getName() {
return name;
}//end getName
public static void setName(String name) {
Novel.name = name;
}//end setName
}//end Novel.class
In order to store an object in a Realm Database, all variables have to have getters/setters. 为了将对象存储在Realm数据库中,所有变量都必须具有getter / setter。 However, since I am using a
RealmList
, how do I structure my getter/setter correctly? 但是,由于我使用的是
RealmList
,如何正确构造我的getter / setter?
Here's the code Android Studio gives me, but it just doesn't look right. 这是Android Studio给我的代码,但是看起来不正确。
public RealmList<Chapter> getChapters() {
return chapters;
}
public void setChapters(RealmList<Chapter> chapters) {
this.chapters = chapters;
}
The getter looks ok, however, the setter makes no sense to me in how it would work with the RealmList
correctly. 吸气剂看起来还不错,但是,对于我来说,如何使用
RealmList
正确设置setter毫无意义。
tldr : What should my getter/setter be in order to properly implement the RealmList
in my custom object? tldr :为了在自定义对象中正确实现
RealmList
,我的getter / setter应该是什么?
RealmList
implements all useful interfaces: RealmList
实现所有有用的接口:
java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>
If you want to add new Chapter
to the Novel
use code similar to: 如果要将新的
Chapter
添加到Novel
使用类似于以下内容的代码:
Chapter chapter = realm.copyToRealm(new Chapter());
novel.getChapters().add(chapter);
If you want to replace all chapters with new one: 如果要用新章节替换所有章节:
RealmList<Chapter> chapters = new RealmList<Chapter>();
chapters.add(new Chapter());
chapters.add(new Chapter());
chapters.add(new Chapter());
chapters.add(new Chapter());
chapters = realm.copyToRealm(chapters);
novel.setChapters(chapters);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.