简体   繁体   English

类型不匹配。 要求:观察员<in Int!>成立:?

[英]Type mismatch. Required: Observer<in Int!> Found:?

I want to Observe live data change in ViewModel and want to change another live data so I am using Mediatorlivedata, I don't know how to observe it in ViewModel, I am getting the compile-time error Type mismatch.我想在 ViewModel 中观察实时数据更改并想更改另一个实时数据,所以我使用 Mediatorlivedata,我不知道如何在 ViewModel 中观察它,我收到编译时错误类型不匹配。 Required: Observer Found: ?要求:发现观察者:?

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"
        }
    })
}

} } 在此处输入图片说明

You Shouldn't observe a live data on the viewModel, try adding it as a source to a mediatorLiveData and observing it directly on the view:您不应在 viewModel 上观察实时数据,请尝试将其作为源添加到 mediatorLiveData 并直接在视图上观察:

val mediatorLiveData: MediatorLiveData<Int> = MediatorLiveData().apply{
   addSource(estimateyearpos) { /*Do something with "integer" */}
}

or even (if you don't need it to be mutable)甚至(如果你不需要它是可变的)

val liveData = Transformations.map(estimateyearpos) { /*Do something with "integer" */}

Both of this options will observe the source live data and apply the given function to it, but you still need to observe it on an Activity or a fragment to properly get the values.这两个选项都将观察源实时数据并将给定的函数应用于它,但您仍然需要在 Activity 或片段上观察它以正确获取值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型不匹配。 必需:ContentResolver! 找到:整数 - Type mismatch. Required: ContentResolver! Found: Int 类型不匹配。 必需:字符串:找到:Int 错误 - Type mismatch. Required: String! Found: Int error 类型不匹配。 必需:片段,找到:PlaceAutocompleteFragment - Type mismatch. Required: Fragment, Found: PlaceAutocompleteFragment 类型不匹配。 必需:找不到:回调&lt;*&gt; - Type mismatch. Required: Nothing Found: Callback<*> 类型不匹配。 必需:找到的上下文:在片段中 - Type mismatch. Required: Context Found: In Fragment 错误:“类型不匹配。必需:观察者<PagedList<MyItem!> !&gt;”。如何解决? - Error: "Type mismatch. Required: Observer<PagedList<MyItem!>!>". How to fix? 类型不匹配。 必需:数组 <Uri> ! 找到:数组 <Uri?> - Type mismatch. Required: Array<Uri>! Found: Array<Uri?> 类型不匹配。 必需: suspend () → Response&lt;*&gt; 找到:Response<Void> - Type mismatch. Required: suspend () → Response<*> Found: Response<Void> 类型不匹配。 必需:结果<newsresponse> : 发现: 结果<response<newsresponse> &gt;? </response<newsresponse></newsresponse> - Type mismatch. Required: Result<NewsResponse>! Found: Result<Response<NewsResponse>>? 类型不匹配。 必需:FirebaseRecyclerAdapter<chatobject, chatvoiceviewholders> ? 成立:</chatobject,> - Type mismatch. Required: FirebaseRecyclerAdapter<ChatObject, ChatVoiceViewHolders>? Found:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM