如何使一个ViewModel类将多个数据类型返回给活动或片段 [英] How can I make one ViewModel class return multiple data types to an activity or a fragment

本文介绍了如何使一个ViewModel类将多个数据类型返回给活动或片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读此博客文章将Android带有Firebase实时数据库的体系结构组件(第2部分),我正在实现最后一个代码片段及其工作方式.

I'm reading this blog post Using Android Architecture Components with Firebase Realtime Database (Part 2) and I'm implementing the last code snippet, and its working.

private final FirebaseQueryLiveData liveData = new FirebaseQueryLiveData(HOT_STOCK_REF);
private final MediatorLiveData<HotStock> hotStockLiveData = new MediatorLiveData<>();

public HotStockViewModel() {
    // Set up the MediatorLiveData to convert DataSnapshot objects into HotStock objects
    hotStockLiveData.addSource(liveData, new Observer<DataSnapshot>() {
        @Override
        public void onChanged(@Nullable final DataSnapshot dataSnapshot) {
            if (dataSnapshot != null) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        hotStockLiveData.postValue(dataSnapshot.getValue(HotStock.class));
                    }
                }).start();
            } else {
                hotStockLiveData.setValue(null);
            }
        }
    });
}

我实际上使用它来返回包装在LiveData中的HotStockViewModel对象的数组(通过遍历数据库的结果,我真的不知道是否应该在ViewModel类中进行此数据操作)并返回将该数组拆分为一个片段,仅显示该片段.

I'm actually using it to return an array of HotStockViewModel objects wrapped in LiveData (by looping through the result from the database and I really don't know if I should be doing this data manipulation in a ViewModel class) and returning the array to a fragment which just displays it.

是否可以使用相同的ViewModel类返回仅包装在LiveData中的价格数组(并且仍然返回包装在LiveData中的HotStockViewModel对象的数组)?

Is it possible to use this same ViewModel class to return an array of only the prices wrapped in LiveData (and still also return an array of the HotStockViewModel objects wrapped in LiveData)?

如果是,该如何实现?我不想返回包装在LiveData中的HotStockViewModel对象的数组,然后循环遍历以获得片段中的价格.我根本不希望该片段执行任何数据操作,而只显示数据.

If it is, how can I achieve this? I don't want to return an array of the HotStockViewModel objects wrapped in LiveData and then loop through it to get the prices in a fragment. I don't want the fragment to do any data manipulation at all, only display data.

我看到了这个stackoverflow问题可以Android架构组件ViewModel是由多个返回LiveData的模型组成的对象吗?但我不明白答案.我仍然是Java初学者.

I saw this stackoverflow question Can an Android Architecture Components ViewModel compose an object from multiple LiveData returning models? but I don't understand the answer. I'm still a Java beginner.

推荐答案

我真的不知道我是否应该在 一个ViewModel类

and I really don't know if I should be doing this data manipulation in a ViewModel class

在我认为您正在使用的Android MVVM模式中,建议不要在Viewmodel类中执行数据操作.而是创建一个应该处理此问题的repo类. viewModel类应该仅从repo类获取数据,然后将其传递给需要它的任何UI组件.

In Android MVVM pattern which i think you are using, it is not advisable to perform data manipulation in the Viewmodel class. Rather create a repo class that should handle this. The viewModel class should only get data from the repo class then passes it to whichever UI components needing it.

这篇关于如何使一个ViewModel类将多个数据类型返回给活动或片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆