如何从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)方法中,我也调用&#34; notifyDataSetChanged()&#34;。
如何根据&#34; isSortAsc&#34;更改LiveData的viewModel类源。参数 (words = isSortAsc?wordRepository.getWordsByAsc():wordRepository.getWordsByDesc()
仅在更改屏幕配置后才能工作,而不是在LiveData字词的insta更改源
之后答案 0 :(得分:2)
一种方法可能是使用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
答案 1 :(得分:0)
我认为这是一个简单的解决方案,总是查询表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);
}
}
我希望这会对你有所帮助。
答案 2 :(得分:0)
使用 Transformations.switchMap 的解决方案
首先制作 >>> import io
>>> audio_raw_data = b'RIFF\xe4\x99\x19\x00WAVEfmt \x10\x00\x00\x00'
>>> type(audio_raw_data)
<class 'bytes'>
>>> audio_file = io.StringIO(audio_raw_data.decode())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 4-5: invalid continuation byte
LiveData
isSortAsc
然后像这样在 private MutableLiveData<Boolean> isSortAsc = new MutableLiveData<Boolean>(true);
上使用 Transformations.switchMap:
isSortAsc
答案 3 :(得分:-1)
我认为需要修改以下功能
from table1 t1,
INNER JOIN table2 t2 ON t2.col1 = t1.col1
LEFT JOIN table3 t3 ON t2.col2 = t3.col2
LEFT JOIN table4 t4 ON t2.col3 = t4.col1
LEFT JOIN table5 t5 ON t2.col4 = t5.col1
and t2.col5 = t5.col2
在触发onChanged之前,单词变量将没有任何值。因此,您必须确保仅在onChanged()
之后调用getWords()