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?