简体   繁体   English

根据 observable 中的数据更新 UI

[英]Update UI with respect to data from observable

I have LiveData in my InfoViewModel (written in Java)我的 InfoViewModel 中有 LiveData(用 Java 编写)

public LiveData<List<Info>> getAllInfo(){ return allInfo }

In my DashboardActivity (written in Kotlin) i am trying to observe this LiveData then use result from the observer to update a list which should in turn update a portion of my UI.在我的 DashboardActivity(用 Kotlin 编写)中,我试图观察这个 LiveData,然后使用观察者的结果来更新一个列表,该列表又应该更新我的 UI 的一部分。

The portion of my UI to be updated when there is a change in infoList size当 infoList 大小发生变化时要更新的 UI 部分

 if(infoList.isEmpty){ Text("Empty") } else { Text("Info list size is ${InfoList.size} ") }

At first i used起初我用

val infoList by remember { mutableStateOf(ArrayList<Info>)}
SideEffect { infoViewModel.allInfo.observe(activity as LifecycleOwner, { infoList.addAll(it) } ) }

But my UI did not update when infoList size was updated by the observer.但是当观察者更新 infoList 大小时,我的 UI 没有更新。 I searched here on stack overflow and found我在这里搜索堆栈溢出并发现

 observeAsState

So i did this所以我做了这个

 val infoList by infoViewModel.allInfo.observeAsState

But then observeAsState show errors.但随后observeAsState 显示错误。 What can i do.我能做些什么。 My main problem here is notifying that portion of my UI that my infoList has changed and it should update itself or at least any way to update my UI with respect to the observable我的主要问题是通知我的 UI 的那部分我的 infoList 已更改,它应该更新自身或至少以任何方式更新我的 UI 相对于 observable

So... I later discovered that所以...我后来发现

  observeAsState

Is part of the是一部分

  androidx.compose.runtime:runtime-livedata

artifacts so i added文物所以我添加了

  implementation "androidx.compose.runtime:runtime-livedata:$compose_version"

to my build.gradle (app level).到我的 build.gradle(应用程序级别)。 I also added我还添加了

  implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha02'

to my build.gradle (app level), converted my DAO, repository and ViewModel classes from Java to Kotlin.到我的 build.gradle(应用程序级别),将我的 DAO、存储库和 ViewModel 类从 Java 转换为 Kotlin。 After doing all these, i was able to在做了所有这些之后,我能够

  import androidx.compose.runtime.livedata.observeAsState

Then on my DashboardActivity, inside my @Composable function然后在我的 DashboardActivity 中,在我的 @Composable function

  val context = LocalContext.current

  val infoViewModel: InfoViewModel = viewModel(factory = InfoViewModelFactory(context.applicationContext as Application))
  /*this particular type of viewModel(factory = ...) is available after adding implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha02' to build.gradle (app level)*/

  val infoList = infoViewModel.allInfo.observeAsState(listOf()).value

Then the portion of my UI that needed to be updated when there is a change in infoList size然后是当 infoList 大小发生变化时需要更新的 UI 部分

  if(infoList.isEmpty){ Text("Empty") } else { Text("Info list size is ${InfoList.size} ") }

Started to work perfectly.开始完美工作。 In essence,在本质上,

  implementation "androidx.compose.runtime:runtime-livedata:$compose_version"

                                      ...

  import androidx.compose.runtime.livedata.observeAsState

                                          ...

  val infoList = infoViewModel.allInfo.observeAsState(listOf()).value

got the job done for me:-)为我完成了工作:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM