androidrealm

How to be with identical in content classes using by Realm


I need to create many identical in content classes like the class below

public abstract class AbstractListModel  extends RealmObject {
@PrimaryKey
private String id;
private String name;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

If I will extend this class in another, it seems I'll have a lot of empty classes, 'cause they have only 2 fields (id and name) which contains in the mother-class.

public class LectureHallListModel extends AbstractListModel {
//@PrimaryKey
//private String id;
//private String name;

//public String getId() {
//    return id;
//}

//public void setId(String id) {
//    this.id = id;
//}

//public String getName() {
//    return name;
//}

//public void setName(String name) {
//    this.name = name;
//}
}

Are the any way to add to a DB several identical in content tables without creation empty classes?

Thank you!


Solution

  • Inheritance of fields (technically from any class that is not directly RealmObject) is not supported by Realm.

    You would need the following setup:

    public interface AbstractListModel {
        String getId();
        void setId(String id);
        String getName();
        void setName(String name);
    }
    

    And

    public class LectureHallListModel extends RealmObject implements AbstractListModel {
        @PrimaryKey
        private String id;
        private String name;
    
        @Override
        public String getId() {
            return id;
        }
    
        @Override
        public void setId(String id) {
            this.id = id;
        }
    
        @Override
        public String getName() {
            return name;
        }
    
        @Override
        public void setName(String name) {
            this.name = name;
        }
    }