Transformations.map 在 Androidx 中使用 ViewModel 和 LiveData(问题)

Transformations.map with ViewModel and LiveData in Androidx (issue)

https://github.com/neuberfran/SmartDrive11/blob/master/app/src/main/java/com/example/neube/smartdrive/controlamotores/SmartViewModel.kt

我有上述项目,基于此:

https://firebase.googleblog.com/2017/12/using-android-architecture-components_20.html

我遇到了这个图像问题:

https://drive.google.com/drive/folders/1_M3KBZnFXkX6I1dMNLejNZAAqrs_QBA9?usp=sharing

有人可以帮忙吗?

您导入了错误的函数类型。它应该是 androidx.arch.core.util.Function 并不是 导入 java.util.function.Function

在你的 Activity 中: 如果您的 ViewModel 是 kotlin,您应该引用 hotStockViewModel.hotStockLiveData 之类的实时数据,因为它是 属性.

在 JVM 上,"hotStockLiveData" 等属性将有一个 "get HotStockLiveData" 和 "setHotStockLiveData",在 kotlin 中被称为 hotStockViewModel.hotStockLiveData,但在 JAVA 中你可以使用 hotStockViewModel.getHotStockLiveData() 这就是为什么您不能在 viewModel 中定义 "getHotStockLiveData()" 函数的原因。