将封装在RealmList中的嵌套Realm对象查询到RealmResults中 [英] Query nested Realm objects encapsulated in RealmList into RealmResults

查看:288
本文介绍了将封装在RealmList中的嵌套Realm对象查询到RealmResults中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下RealmObject:

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;
    }
}

现在有了cityId,我需要查询特定城市的街道.怎么做?我尝试过的是:

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()

但这会导致异常.我需要在实现过滤的ListView中显示街道,所以我需要街道为RealmResults才能使用RealmBaseAdapter<Street>.

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>.

推荐答案

正确的方法是在onCreate()的Activity中打开并在onDestroy()

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();

然后,您可以像访问其他列表一样访问RealmList<T>

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

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

然后,您可以使用recyclerview获取streets列表中给定索引的视图.

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

这篇关于将封装在RealmList中的嵌套Realm对象查询到RealmResults中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆