Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert RealmResults<E> to List<E> with copyFromRealm

Tags:

android

realm

How can I convert RealmResults<E> to List<E>?

I tried with method 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"

But not quite understand why in copy getTitle() give correct result, but getId() incorrect.

Model

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;
        }
  }
like image 986
Alexandr Avatar asked Feb 16 '16 08:02

Alexandr


1 Answers

The problem is your setId method.

Right now it does this:

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

It should be

public void setId(int id) {
  this.id = id;
}
like image 155
Christian Melchior Avatar answered Oct 12 '22 23:10

Christian Melchior