MediatorLiveData onChanged无法通过ROOM + Viewmodel触发

时间:2019-05-16 18:04:41

标签: java android

我尝试实现MediatorLiveData来侦听由于查询调用而引起的LiveData中的更改。

我尝试附加一个观察者,但是onchanged方法永远不会触发

我的MainActivity:

final MediatorLiveData<List<Reminder>> liveDataMerger = new 
final LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);

liveDataMerger.observe(new LifecycleOwner() {
            @NonNull
            @Override
            public Lifecycle getLifecycle() {
                return lifecycleRegistry;
            }
        }, new Observer<List<Reminder>>() {
            @Override
            public void onChanged(List<Reminder> reminders) {
                if(reminders != null) {
                    reminderAdapter.setAdapterlist(reminders);
                    reminderAdapter.notifyDataSetChanged();
                }
            }
        });



        viewModel.getLiveReminderList().observe(this, new Observer<List<Reminder>>() {
            @Override
            public void onChanged(List<Reminder> reminders) {
                if(reminders != null) {
                    reminderAdapter.setAdapterlist(reminders);
                    reminderAdapter.notifyDataSetChanged();
                }
            }
        });


        liveDataMerger.addSource(viewModel.getLiveReminderList(),new Observer<List<Reminder>>() {
            @Override
            public void onChanged(List<Reminder> reminders) {
                int i = 121;
                if(reminders != null) {
                    liveDataMerger.setValue(reminders);
                    reminderAdapter.setAdapterlist(reminders);
                    reminderAdapter.notifyDataSetChanged();
                }
            }
        });

我想触发改变的方法。

0 个答案:

没有答案