Android 编辑文本并将字符串传递给ViewModel?
我正在创建一个使用天气API的应用程序,我需要从UI中的编辑文本到ViewModel中获取地点的名称,还有一个val,它从存储库中获取方法。在我的例子中,如何正确地与ViewModel通信?LiveData中有一些魔力,或者我需要数据绑定 视图模型:Android 编辑文本并将字符串传递给ViewModel?,android,android-livedata,android-viewmodel,Android,Android Livedata,Android Viewmodel,我正在创建一个使用天气API的应用程序,我需要从UI中的编辑文本到ViewModel中获取地点的名称,还有一个val,它从存储库中获取方法。在我的例子中,如何正确地与ViewModel通信?LiveData中有一些魔力,或者我需要数据绑定 视图模型: class MainViewModel( private val weatherRepository: WeatherRepository ) : ViewModel() { val metric: String = "metric" val
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中观察一些实时数据方法1()->通过方法2(城市)更改viewmodel中的实时数据状态听起来很有趣。你能给我看一些例子,短代码等吗。?