Good day. I master the ORM Realm. Faced with an incomprehensible error to me. There is an adapter class for recyclerView, here's a piece of code.

List<Dog> dogs = Collections.emptyList(); public void addDog(Dog dog) { dogs.add(dog); this.notifyDataSetChanged(); } public RecycleViewAdapter(List<Dog> dogs) { this.dogs = dogs; } 

Next, here is the Dog class itself.

 import io.realm.RealmObject; public class Dog extends RealmObject{ private String name; private int age; public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } @Override public String toString(){ return "!!! Name - "+name+" | Age - "+age; } } 

Adding new dogs in the database is as follows

 public void addDog() { realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { Dog dog = realm.createObject(Dog.class); dog.setName(nameDog.getText().toString()); dog.setAge(Integer.parseInt(ageDog.getText().toString())); } }); } 

, at this stage everything is ok. After added to the database, a notification of this comes, I look at what has changed and try to change the data in the adapter RecyclerView, but then an annoying error falls out here is the code in which notifications come

 final RealmResults<Dog> puppies = realm.where(Dog.class).lessThan("age", 7).findAll(); puppies.addChangeListener(new OrderedRealmCollectionChangeListener<RealmResults<Dog>>() { @Override public void onChange(RealmResults<Dog> dogs, @Nullable OrderedCollectionChangeSet changeSet) { int[] s = changeSet.getInsertions(); adapter.addDog(dogs.last()); } }); 

An error occurs here adapter.addDog(dogs.last());

 java.lang.UnsupportedOperationException: This method is not supported by 'RealmResults' or 'OrderedRealmCollectionSnapshot'. 

It turns out that List dogs in the constructor of the adapter can be safely assigned the value RealmResults. But here it is already impossible to use the add method for List dogs. Please tell me why this error falls, how to fix it?

    2 answers 2

    RealmResults stores a real-time sample of a database. We need to add items to it, because the database itself does this. You generally do not need to manually add something to the list. he automatically adds / deletes elements. Therefore, you only need to notify the adapter of data changes in onChange .

     final RealmResults<Dog> puppies = realm.where(Dog.class).lessThan("age", 7).findAll(); puppies.addChangeListener(new OrderedRealmCollectionChangeListener<RealmResults<Dog>>() { @Override public void onChange(RealmResults<Dog> dogs, @Nullable OrderedCollectionChangeSet changeSet) { adapter.notifyDataSetChanged(); } }); 
    • one
      it worked, awesome, put a like. - Nikita

    To make things easier, use RealmRecyclerViewAdapter . Then you will not need to be notified of changes in the data. Just work with the database, and the list is updated automatically.

    An example implementation can be found here .