【问题标题】:Convert RealmResults<E> to List<E> with copyFromRealm使用 copyFromRealm 将 RealmResults<E> 转换为 List<E>
【发布时间】:2016-02-16 08:54:57
【问题描述】:

如何将RealmResults&lt;E&gt; 转换为List&lt;E&gt;

我尝试了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;
        }
  }

【问题讨论】:

    标签: android realm


    【解决方案1】:

    问题在于您的setId 方法。

    现在它是这样做的:

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

    应该是

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

    【讨论】:

    • 很惭愧,很抱歉浪费了您的时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2017-04-07
    • 1970-01-01
    • 2015-09-19
    • 2015-01-27
    • 1970-01-01
    相关资源
    最近更新 更多