簡體   English   中英

如何從 Room Dao 更改我的 LiveData 的 ViewModel 源

[英]How can I change in ViewModel source of my LiveData from Room Dao

如何從 Room Dao 更改我的 LiveData 的 ViewModel 源。 在 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();
}

我也有 Repository 類:

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

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

和我的 ViewModel 類:

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()”。

LiveData 的 viewModel 類源如何根據“isSortAsc”參數發生變化(words = isSortAsc ? wordRepository.getWordsByAsc() : wordRepository.getWordsByDesc()

它僅在更改屏幕配置后才有效,而不是在更改 LiveData 單詞的源后

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

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

我在下面做了類似的事情(設置directionhttps://github.com/joreilly/galway-bus-android/blob/master/app/src/main/java/com/surrus/galwaybus/ui/viewmodel/ BusStopsViewModel.kt

使用Transformations.switchMap的解決方案

首先 make isSortAsc LiveData

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

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

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

我認為這是一個簡單的解決方案,始終查詢表 ASC ie 並僅在用戶選擇降序的情況下反轉列表。 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 之前, words 變量不會有任何值。 所以你必須確保只有在 onChanged() 之后才應該調用 getWords()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM