android - 编辑文本并将字符串传递给 ViewModel?
问题描述
我正在创建使用天气 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)
}
}
解决方案
使用实时数据。在ui中观察一些实时数据method1()->通过method2(city)更改viewmodel中的livedata状态
推荐阅读
- scala - 如何解决scala中的错误“类型不匹配,预期:_$1,实际:任何”?
- typescript - 为什么编译后内容脚本不加载?
- r - 创建新列,设置值等于 R 中单独列表中的静态值
- unity3d - 有没有办法给脸而不是顶点着色?
- reactjs - 尝试在 userEffect 上定义对象数组的反应最大状态更新错误
- java - 在比较器中使用字符串时出错
- sql - 如何处理一些匹配正则表达式的字符串?
- pytorch - 如何使用 PyTorch DataLoader 进行强化学习?
- swiftui - 如何根据某个标签值返回不同的视图?
- html - 当我将自己的 3D 模型添加到
用于场景查看器的 html 中的标签,模型不加载