我怎样才能转换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"
Run Code Online (Sandbox Code Playgroud)
但不太明白为什么在副本中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;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是你的setId方法.
现在它这样做:
public void setId(int id) {
this.id = this.id;
}
Run Code Online (Sandbox Code Playgroud)
它应该是
public void setId(int id) {
this.id = id;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4599 次 |
| 最近记录: |