當我啓動Android應用程序時,登錄到Realm Object Server並從Realm Server Object下載(同步)數據。好的。 現在我到了顯示人員列表的片段。遠程領域(在領域對象服務器上)發生變化時不通知片段
下面的代碼:
public class PersonsFragment extends Fragment {
private ListView notEmptytsListView;
private Realm realm;
private RealmResults<Person> personsRealmResults;
private PersonsAdapter personsAdapter; // RealmBaseAdapter<Organization>
private RealmChangeListener realmChangeListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.persons_fragment, container, false);
init(rootView);
return rootView;
}
private void init(View view) {
realm = Realm.getDefaultInstance();
notEmptytsListView = (ListView) view.findViewById(R.id.notEmptytsListView);
personsRealmResults = realm.where(Person.class).findAllSorted(Person.NAME);
personsAdapter = new PersonsAdapter(personsRealmResults);
notEmptytsListView.setAdapter(personsAdapter);
// NOT CALL WHEN DATA ON REALM OBJECT SERVER WAS CHANGED
realmChangeListener = new RealmChangeListener() {
@Override
public void onChange(Object object) {
personsAdapter.notifyDataSetChanged();
}
};
personsRealmResults.addChangeListener(realmChangeListener);
}
@Override
public void onDestroy() {
super.onDestroy();
realm.removeChangeListener(realmChangeListener);
realm.close();
}
}
如果我留在片段和數據變化領域對象服務器(遠程域)的本地數據不同步的。不調用方法onChanged()。
我該如何解決這個問題?
您的部分代碼未格式化,請進行校對。 – SteveFest
您需要將RealmResults存儲爲字段變量,而不僅僅是局部變量。 – EpicPandaForce