【问题标题】:Edit Text and pass string to the ViewModel?编辑文本并将字符串传递给 ViewModel?
【发布时间】: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


    【解决方案1】:

    使用实时数据。在 ui 中观察一些实时数据 method1() -> 通过 method2(city) 更改 viewmodel 中的实时数据状态

    【讨论】:

    • 听起来很有趣。你能给我看一些例子,短代码等吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多