简体   繁体   English

Android ViewModel,项目列表,插入-处理状态

[英]Android ViewModel, List of items, inserting - handling state

My question is kind of not strictly precised. 我的问题不是严格严格的。 I have a Fragment with list of items that are backed up by REST API service: 我有一个片段,其中包含由REST API服务备份的项目列表:

GET /api/items

I have a ItemsViewModel class, ItemsProvider with LiveData objects and APIService that get the items from REST service. 我有一个ItemsViewModel类,带有LiveData对象的ItemsProvider和AP​​IService,它们从REST服务获取项目。 The implementation of ListFragment observes the ItemsViewModel and its state. ListFragment的实现观察ItemsItemModel及其状态。

Now I have a question. 现在我有一个问题。 How do you implement inserting data into ViewModel in your implementations? 在实现中如何实现将数据插入ViewModel中? How do you handle state update? 您如何处理状态更新?

You can take LiveData<List<? of items>> 您可以使用LiveData<List<? of items>> LiveData<List<? of items>> as MutableLiveData inside ItemsViewModel . LiveData<List<? of items>>作为MutableLiveData ItemsViewModel

Now, when you've new items, you need to set your LiveData value. 现在,当您有新项目时,需要设置LiveData值。

You need to create an observer to that inside of your activity/fragment where you needed. 您需要在您需要的activity/fragment内部创建一个observer

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

相关问题 动态插入列表项到android中的listview - Dynamically inserting list items to listview in android 使用 android Jetpack compose 中的列表处理 state - Handling state using list in android Jetpack compose Android-保存活动状态并列出视图项 - Android - save activity state and list view items Android [Kotlin] - 使用共享视图模型将新项目插入到 MasterDetail-Flow 上的选定子列表中 - Android [Kotlin] - Inserting a new item into a selected Child-List on a MasterDetail-Flow using shared viewModel 如何在设备旋转时恢复 Android RecyclerView 列表项中的复选框状态 - How to restore checkBox state in Android RecyclerView list items on Rotation of device android列表项简单TextView滚动后更改为初始状态 - android list items simple TextView change to initial state after scrolling 在Android ICS中插入日历项目 - Inserting Calendar items in Android ICS DynamoDB 嵌套项目插入 Android - DynamoDB nested items inserting Android Android使用ViewModel存储片段UI状态 - Android using viewmodel to store fragment UI state 在 Android 的 MVVM 中将 ViewModel State 保存在哪里? - Where to Save ViewModel State in Android's MVVM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM