onChange方法不起作用

时间:2016-08-01 07:41:43

标签: java android realm

我在下面的活动中得到一个RealmResults:

citySceneryBeanRealmResults = realm.where(CitySceneryBean.class).equalTo("cityid", pp.getCurrentCityId()).findAll()  

然后我在另一个B Activity中获得一个RealmObject,并将realmobject设置为RealmChangeListener,如下所示:

bean = realm.where(CitySceneryBean.class).equalTo("id", mVid).findFirst();
bean.addChangeListener(new RealmChangeListener<CitySceneryBean>(){
        @Override
        public void onChange(CitySceneryBean element) {
            Log.i(SceneryDetailActivity.class, "onChange");
            noManagedBean = realm.copyFromRealm(element);
            refreshData();
        }
    });  

我确认ReamlObject“bean”包含在citySceneryBeanRealmResults中,现在当citySceneryBeanRealmResults被修改时,在A Activity中我确认相关的bean被更新了,但是在B Activity中onChange方法不起作用,为什么?

1 个答案:

答案 0 :(得分:0)

您应确保RealmChangeListener realmChangeListener = new RealmChangeListener<CitySceneryBean>(){ @Override public void onChange(CitySceneryBean element) { Log.i(SceneryDetailActivity.class, "onChange"); noManagedBean = realm.copyFromRealm(element); refreshData(); } }; void someMethod() { bean = realm.where(CitySceneryBean.class).equalTo("id", mVid).findFirst(); bean.addChangeListener(realmChangeListener); } 是字段变量。

<SchoolName>
  <name>ABC International, Wisconsin</name>
  <rank>10</rank>
</SchoolName>
<SchoolName>
  <name>XYZ Primary, Las Vegas</name>
  <rank>4</rank>
</SchoolName>
<SchoolName>
  <name>Ryan Academy, Wisconsin</name>
  <rank>6</rank>
</SchoolName>
<SchoolName>
  <name>Advanced Elementary, Houston</name>
  <rank>15</rank>
</SchoolName>