Query nested Realm objects encapsulated in RealmList into RealmResults

点点圈 提交于 2020-01-14 09:44:18

问题


I have the following RealmObject:

public class City extends RealmObject {
    private String cityId;
    private RealmList<Street> streets;

    public String getId() {
        return cityId;
    }

    public void setCityId(String cityId) {
        this.cityId = cityId;
    }

    public RealmList<Street> getStreets() {
        return streets;
    }

    public void setStreets(RealmList<Street> streets) {
        this.streets = streets;
    }
}

Now having a cityId I need to query streets of particular city. How to do that? What I did try was:

    Realm.getInstance(context).where(City.class).equalTo("cityId", someCityId, false)
         .findFirst().getStreets().where().findAll()

But this leads into an Exception. I need to display streets in a ListView implementing filtering so I need streets to be RealmResults to use RealmBaseAdapter<Street>.


回答1:


The proper way would be to have an open Realm instance either opened in your Activity in onCreate() and closed in onDestroy(), or in your custom application class.

Then you can use this realm instance to query the realm

City city = realm.where(City.class).equalTo("cityId", cityId).findFirst();

Then you can access the RealmList<T> like any other list

RealmList<Street> streets = city.getStreets();

Then you can use a recyclerview to get the view for a given index within your streets list.



来源:https://stackoverflow.com/questions/31806848/query-nested-realm-objects-encapsulated-in-realmlist-into-realmresults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!