[英]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和APIService,它们从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.