Search code examples
androidandroid-studio-3.0realm-mobile-platformrealm-list

Update the list when data is filtered from the realm database in android


i am stucked with the list update problem when i filter the list of items from realm database.

What exactly i am trying to do is add some user records in the table and when user click on filter button, i query realm database to filter the results. I am able to get the results printed in the logs but that donot reflect in the list as change listener is not called because of no change in database. Can any one guide how to reflect the results obtained through query in the list?

Model class is as shown below.

public class UserModel extends RealmObject {

    @PrimaryKey
    private int id;

    @Required
    private String firstName;

    @Required
    private String lastName;

    private UserAddressModel userAddress;

    private int age;

    public UserAddressModel getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(UserAddressModel userAddress) {
        this.userAddress = userAddress;
    }

    public int getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Query to add record in database is as below.

realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(@NonNull Realm realm) {
                int nextID;
                if (mUserModel == null) {
                    // increment index
                    Number num = realm.where(UserModel.class).max("id");

                    if (num == null) {
                        nextID = 1;
                    } else {
                        nextID = num.intValue() + 1;
                    }

                } else {
                    nextID = mUserModel.getId();
                }
                UserModel obj = new UserModel();
                obj.setId(nextID);
                obj.setFirstName(etFirstName.getText().toString().trim());
                obj.setLastName(etLastName.getText().toString().trim());
                obj.setAge(Integer.parseInt(etAge.getText().toString().trim()));

                UserAddressModel userAddressModel = new UserAddressModel();
                userAddressModel.setAddress(etAddress.getText().toString().trim());
                userAddressModel.setId(nextID);
                obj.setUserAddress(userAddressModel);
                realm.copyToRealmOrUpdate(obj);
              realm.beginTransaction();                  
                realm.addChangeListener(mOnDataChangeListener);
            }
        });

Query to filter record is as below.

RealmResults<UserModel> realmList = realm.where(UserModel.class).greaterThanOrEqualTo("age", 20).findAll();

Above query gives the desired result as i had printed it in log. But dont update the list.

I had also tried adding call listener to the above query and notify data set change to the list, but it didn't help.

Any help would be appreciated. TIA.


Solution

  • Ok so finally found a solution to the about question after expermenting various things. All you need to do is call updateData() method of the RealmRecyclerViewAdapter which you will extend to your normal recyclerview.

    Below code snippet provide the solution.

    realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(@NonNull Realm realm) {
                        userModels = realm.where(UserModel.class).greaterThanOrEqualTo("age", 20).findAll();
                    }
                });
                dataListAdapter.updateData(userModels);
    

    The main point to keep in mind in above code is that you have to notify data outside the transaction block. If not done outside block, app will crash stating you cannot notify data when transaction is running.

    Hope it helps someone somewhere.

    Happy coding!!!