如何从房间道更改我的实时数据的视图模型源



如何从房间道更改我的实时数据的视图模型源。在WordDao中,我有两个疑问:

@Dao
public interface WordDao {
   @Query("SELECT * FROM " + Word.TABLE_NAME + " ORDER BY text ASC")
   LiveData<List<Word>> getWordsByAsc();
   @Query("SELECT * FROM " + Word.TABLE_NAME + " ORDER BY text DESC")
   LiveData<List<Word>> getWordsByDesc();
}

我还有存储库类:

public class WordRepository {
   public LiveData<List<Word>> getWordsByAsc() {
       return wordDao.getWordsByAsc();
   }
   public LiveData<List<Word>> getWordsByDesc() {
       return wordDao.getWordsByDesc();
   }
}

和我的视图模型类:

public class WordViewModel extends AndroidViewModel {
    private boolean isSortAsc = true;
    private LiveData<Word> words;
    private WordRepository wordRepository;
    public WordViewModel(@NonNull Application application) {
        super(application);
        wordRepository = new WordRepository(application);
        words = wordRepository.getWordsByAsc();
    }
    public LiveData<List<Word>> getWords() {
       return words;
    }
    public void sortButtonClicked() {
       isSortAsc = !isSortAsc;
       //TODO how change here source of data depending on the isSortAsc
       //It works only after change configuration of screen, not insta
       words = isSortAsc ? wordRepository.getWordsByAsc() : 
            wordRepository.getWordsByDesc()
    }
}

在我的活动中,我添加了观察者:

 wordViewModel.getWords().observe(this, new Observer<List<Word>>() {
      @Override
      public void onChanged(@Nullable List<Word> words) {
          adapter.setWords(words);
      }
 });

在setWords(words(方法中,我也调用"notifyDataSetChanged(("。

如何根据"isSortAsc"参数更改LiveData的视图模型类源(words = isSortAsc ? wordRepository.getWordsByAsc(( : wordRepository.getWordsByDesc((

它仅在更改屏幕配置后起作用,而不是在 insta 更改 LiveData 单词的源后工作

一种方法可能是使用MediatorLiveData....例如:

val words = MediatorLiveData<Word>().apply {
    this.addSource(sortDirection) {
        if (sortDirection.value) {
             this.value = wordRepository.getWordsByAsc()
        } else {
             this.value = wordRepository.getWordsByDesc()
        }
    }
}

我在以下方面做了类似的事情(设置direction(https://github.com/joreilly/galway-bus-android/blob/master/app/src/main/java/com/surrus/galwaybus/ui/viewmodel/BusStopsViewModel.kt

使用 Transformations.switchMap 的解决方案

首先制作isSortAsc实时数据

private MutableLiveData<Boolean> isSortAsc = new MutableLiveData<Boolean>(true);

然后在isSortAsc上使用 Transformations.switchMap,如下所示:

words = Transformations.switchMap(isSortAsc, 
    value -> value ? wordRepository.getWordsByAsc() : wordRepository.getWordsByDesc())
我认为

这是简单的解决方案,始终查询表 ASC 即仅在用户选择降序的情况下反转列表。Collection.Sort(( 由 Android 提供,因此它的性能应该更好。

 public void sortButtonClicked() {
       isSortAsc = !isSortAsc;
       //TODO how change here source of data depending on the isSortAsc
       //It works only after change configuration of screen, not insta
 words = wordRepository.getWordsByAsc();
      if(!isSortAsc) {
         Collections.reverse(words);
       }
}

我希望这对你有帮助。

我认为以下函数需要修改

wordViewModel.getWords().observe(this, new Observer<List<Word>>() {
      @Override
      public void onChanged(@Nullable List<Word> words) {
          adapter.setAdapter(words);
          notifyDataSetChanged();
      }
 });

还有一件事,单词变量在触发 onChanged 之前不会有任何值。所以你必须确保getWords((应该只在onChanged((之后调用。

相关内容

  • 没有找到相关文章