如何将RealmResults<E>转换为List<E>

我尝试了copyFromRealm方法:

RealmResults<EventRealm> result = realm.where(EventRealm.class).findAll();

EventRealm eventRealm = result.get(0);
int id = eventRealm.getId(); // return id 2564
String title = eventRealm.getTitle(); // return "My event"

List<EventRealm> copied = realm.copyFromRealm(result);

EventRealm eventRealm1 = copied.get(0);
int id1 = eventRealm1.getId(); // return id 0
String title1 = eventRealm1.getTitle(); // return "My event"

但是不太了解为什么在副本getTitle()中给出正确的结果,但是getId()不正确。

模型
public class EventRealm extends RealmObject {

        @PrimaryKey
        private int id;
        private String title;

        public int getId() {
            return id;
        }

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

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
  }

最佳答案

问题是您的setId方法。

现在,它这样做:

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

它应该是
public void setId(int id) {
  this.id = id;
}

关于android - 使用copyFromRealm将RealmResults <E>转换为List <E>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35427878/

10-09 21:02