我想观察ViewModel中的实时数据更改,并想要更改另一个实时数据,所以我正在使用Mediatorlivedata,我不知道如何在ViewModel中观察它,我收到了编译时错误类型不匹配。必需:找到观察员:?
class CheckmeasureViewModel(private val repository: UserRepository) : ViewModel() {
var estimateFinancialyear: ArrayList<FinYear> = ArrayList()
var asset = arrayListOf("Select")
var estimate = arrayListOf("Select")
var appPref: AppPref
var estimateyearpos = MutableLiveData<Int>()
var mediatorLiveData: MediatorLiveData<Int> = MediatorLiveData()
init {
appPref = AppPref.getInstance()!!
estimateFinancialyear.add(FinYear(0, "Select"))
estimateFinancialyear.addAll(repository.getFinYears())
estimateyearpos.observeForever(object : Observer<in Int> {
fun onChanged(@Nullable integer: Int?) { //Do something with "integer"
}
})
}
您不应该在viewModel上观察实时数据,尝试将其作为源添加到mediatorLiveData并直接在视图上观察它: