android - MVVM体系结构中的ViewModel操作

标签 android mvvm viewmodel android-room

我的问题是,我需要先对 session 室数据库中的数据进行预处理,然后才能在 View 中显示。

因此,这里是我的应用程序的上下文:

我正在编写“记录卡” android应用程序。因此,我有一个房间数据库,其中存储了我所有的记录卡。实体中的信息是:

@Entitiy:
- ID
- Question
- Answer
- Topic
- Boxnumber (Box depends on how often i was right with my Answer)

我在实体周围有正常的房间设置,我在几个教程中发现了:Dao, Database, Repository。此外,我有一个ViewModel连接到Repository。还有一个View,用于显示连接到ViewModel的当前Question。

我的主意:

我的想法是ViewModel可以保存所有需要的卡片的LiveData(例如,带有特定主题)。
我需要针对该数据的某种算法,该算法将选择View所需的下一张卡片。这取决于:多少张卡具有不同的箱号,哪些卡是最近的10个问题,等等。

我的问题:

在我的ViewModel中,我从LiveData接收repository。每个教程仅显示带有 View 的Room-Database数据。但是我需要在ViewModel内部做一些预处理。使用LiveData访问.getValue()不起作用,仅返回null对象。观察ViewModel中的数据也不起作用,因为为此您需要一个Activity

我不知道我应该将算法与数据库中的数据一起放置在哪里。我不想将其放在View中,因为我想在ViewModel中保存当前算法参数。

一些代码可以更好地理解我的程序:

@道
@Query("SELECT * FROM Karteikarte WHERE BoxnummerMixed = :Boxnummer")
LiveData<List<Karteikarte>> getKarteikartenInBoxMixed(int Boxnummer);

@Query("SELECT * FROM Karteikarte WHERE BoxnummerTopic = :Boxnummer AND Thema = :thema")
LiveData<List<Karteikarte>> getKarteikartenInBoxTopic(int Boxnummer, int thema);

资料库
public LiveData<List<Karteikarte>> getKarteikarteInBoxMixed(int boxnummer){
    return karteikarteDao.getKarteikartenInBoxMixed(boxnummer);
}

public LiveData<List<Karteikarte>> getKarteikarteInBoxTopic(Thema thema, int boxnummer){
    return karteikarteDao.getKarteikartenInBoxTopic(thema.ordinal(), boxnummer);
}

View 模型
public LearningViewModel(Application application){
    super(application);
    repository = new KarteikarteRepository(application);
}

//This method will be called from the View at onCreate
public void initLearning(){
    allCards = repository.getKarteikarteInBoxMixed(1);
}

//This method will be called from the View
public Karteikarte nextCard() {
        // here will be a more complex algorithm
        Random random = new Random();
        List<Karteikarte> list = allCards.getValue();
        return list.get(random.nextInt(list.size()));
}

最佳答案

在这里我用了Transformations.switchMap()Transformations.map()

要详细了解Transformations和MediatorLiveData,可以观看 Android Dev Summit '18上的此视频-Fun with LiveData

private MutableLiveData<Integer> boxNumberLiveData = new MutableLiveData<>();
private final LiveData<List<Karteikarte>> allCardsLiveData;

//Observe this from Fragment or Activity
public final LiveData<Karteikarte> karteikarteLiveData;

LearningViewModel(){

    // when ever you change box number below function is called and list of Karteikarte will be updated
    allCardsLiveData = Transformations.switchMap(boxNumberLiveData, new Function<Integer, LiveData<List<Karteikarte>>>() {
        @Override
        public LiveData<List<Karteikarte>> apply(Integer number) {
            if(number == null)return null;
            return repository.getKarteikarteInBoxMixed(1);
        }
    });

    // when ever list of Karteikarte is changed, below function will be called and a random Karteikarte will be selected
    karteikarteLiveData = Transformations.map(allCardsLiveData, new Function<List<Karteikarte>, Karteikarte>() {
        @Override
        public Karteikarte apply(List<Karteikarte> list) {
            return getRandomKarteikarte(list);
        }
    });

}

public void initLearning(){
    //Enter box number here
    // you can modify box number anytime you want, everything will change respectively
    boxNumberLiveData.setValue(1);
}


private Karteikarte getRandomKarteikarte(@Nullable List<Karteikarte> list){
    if(list == null)return null;
    Random random = new Random();
    return list.get(random.nextInt(list.size()));
}

如果您想获得下一个随机的Karteikarte。最好的实现方法是在数据库中有一个额外的字段。
@Entitiy:
- ID
- Question
- Answer
- Topic
- Boxnumber (Box depends on how often i was right with my Answer)
- isShown(boolean)

在查询数据时,您需要添加isShown is false的额外条件
因此,当您要加载下一个Karteikarte时,只需将当前Karteikarte的isShown字段更新为true

如果要重新使用已经显示的Karteikarte,则可以在开始过程时将isShown值重置为false

关于android - MVVM体系结构中的ViewModel操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53261887/

相关文章:

c# - 不同ViewModel的访问属性

wpf - 当通过DataTemplate应用时,为什么没有代码隐藏我的WPF View 不起作用?

c# - 在另一个 ViewModel 的 PropertyChanged 上更新另一个 ViewModel

java - 时间戳方法 'SimpleDateFormat' 未从数据库检索

android - phonegap 开发者应用程序的行为与已安装的 apk 不同

wpf - 单击标签以使用 MVVM 在 WPF 中选择单选按钮

wpf - 我应该如何实现应用程序范围的字体大小设置和允许用户选择该字体大小的对话框?

java - 如果 ViewModel 持有此 Activity 实现的接口(interface)引用,GC 是否会收集 Activity 引用?

android - Google Play 控制台 - 内部测试链接不起作用

java - Android 构建错误 com.google.firebase :firebase-core:16. 0.8