【发布时间】:2019-08-14 17:52:19
【问题描述】:
我正在创建使用天气 API 的应用程序,我需要从 UI 中的编辑文本到 ViewModel 中获取地点名称,并且有从存储库获取方法的 val。在我的情况下如何与 ViewModel 正确通信? LiveData 中有一些魔法咒语还是我需要数据绑定?
视图模型:
class MainViewModel(
private val weatherRepository: WeatherRepository
) : ViewModel() {
val metric: String = "metric"
val currentWeatherByCoordinates by lazyDeferred {
weatherRepository.getCurrentWeather() }
val forecastByCoordinates by lazyDeferred {
weatherRepository.getForecast() }
val currentWeatherByCity by lazyDeferred { //This is what I'm writing about
weatherRepository.getCurrentWeatherByCity("London", metric)
}
}
【问题讨论】:
标签: android android-livedata android-viewmodel