简体   繁体   English

如何在我的自定义对象中正确实现领域列表?

[英]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.

相关问题 如何正确将List转换为我的Object类型的ArrayList? - How do I properly convert List to ArrayList of my type of Object? 如何使我的领域对象成为Java对象? - How do I make my realm object a java object? 如何使用内部类正确实现Java接口? - How do I properly implement my Java interface with inner class? 如何正确地为我的 Jcomponent 实现鼠标侦听器? - How do I properly implement a mouselistener to my Jcomponent? 如何在 Realm 浏览器中查看我的 Realm 文件? - How do I view my Realm file in the Realm Browser? 如何使用自定义链接列表实施排序? - How should I implement sorting with my custom linked list? 将对象转换为 Realm 中的列表 [对象] 作为迁移(如何在 Realm 中将列表 [对象] 替换为对象字段?) - Transform an object to a list[object] in Realm as migration ( How can i replace a list[objects] with object field in Realm? ) Streams API :: 如何从列表中修改自定义对象的变量<Object> ? - Streams API :: How do i modify the variables of my custom object from a LIST<Object>? 如何正确测试自定义对象中的 char[] 字段是否为空值、空格或空值? 为什么 char[ ] 表示长度为 11? - How do I properly test the char[ ] fields in my custom object for nulls, blanks, or empty? and Why do char[ ] indicate a length of 11? 如何使用接口正确实现多态? - How do I implement polymorphism properly with Interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM