0

我有两个实时数据对象,如下所示:

private val _hours: MutableLiveData<MutableList<Hour>>
    get() =
        MutableLiveData<MutableList<Hour>>().apply {
            value = HourRepo.getHours(getApplication())
        }
private val _selectedHours: MutableLiveData<MutableList<Hour>>
    get() =
        MutableLiveData<MutableList<Hour>>().apply {
            value = AppData.selectedHours
        }

我更改了这些 LiveData 值的一些属性,如下所示:

fun checkHour(hour: Hour) {

    _hours.value?.find { it -> hour == it }?.isChecked =
        !_hours.value?.find { it -> hour == it }?.isChecked!!

    if (hour.isChecked) {

        _selectedHours.value?.add(hour)
    } else {

        _selectedHours.value?.remove(hour)
    }

    _selectedHours.notifyObserver()
    _hours.notifyObserver()

}

我尝试在使用此扩展功能进行这些更改后触发观察者:

private fun <T> MutableLiveData<T>.notifyObserver() {
    this.value = this.value
}

但是,不会调用观察者。这是观察者的代码:

 studentViewModel = (activity as Homepage).getViewModel()

    studentViewModel.getHours(currentDay).observe(viewLifecycleOwner, Observer {
        hourAdapter.submitList(it)
    })

我一直在寻找答案,但没有任何效果。可能是什么问题呢?

4

0 回答 0