[英]Changing data in Live Data Kotlin
我有allRecords
- 通過存儲庫從房間獲得的實時數據值。
如果LiveData<List<...>>
與 id 匹配,我希望handleSelectedItem
方法更改其中一項的值。 我試圖用 Transformation.map() 做到這一點,但是這段代碼不起作用
class RecordListViewModel @Inject constructor(val repository: RecordRepository): ViewModel() {
private var allRecords : LiveData<List<RecordItem>> = Transformations.map(repository.getAllRecording()){
records -> return@map records.map{ it.toItem()}
}
fun getAllRecords() : LiveData<List<RecordItem>>{
return allRecords
}
fun handleSelectedItem(id : Int) {
Log.d("HandleSelectedItem", "Test1")
allRecords = Transformations.map(allRecords) { records ->
return@map records.map {
if (it.id == id){
Log.d("HandleSelectedItem", "Test2")
it.copy(isSelected = true)
}
else{
Log.d("HandleSelectedItem", "Test3")
it.copy(isSelected = false)
}
}
}
}
}
幫助解決這個問題
更新這里提供了MutableLiveData
而不是LiveData
。 然后 Repository 和 Dao 都應該返回 MutableLiveData
存儲庫
fun getAllRecording(): MutableLiveData<List<RecordEntity>> =
appDatabase.getRecordDao().getAllRecording()
道
@Query("SELECT * FROM record")
fun getAllRecording() : MutableLiveData<List<RecordEntity>>
但 Room 數據庫無法返回 MutableLiveData
錯誤
D:\Project\VoiceRecording\app\build\tmp\kapt3\stubs\debug\ru\ddstudio\voicerecording\data\database\daos\RecordDao.java:17: error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.MutableLiveData<java.util.List<ru.ddstudio.voicerecording.data.database.entities.RecordEntity>>).
public abstract androidx.lifecycle.MutableLiveData<java.util.List<ru.ddstudio.voicerecording.data.database.entities.RecordEntity>> getAllRecording();
更新2
private val allRecords = MediatorLiveData<List<RecordItem>>().apply {
val recordsRepository = repository.getAllRecording().map { records -> records.map { it.toItem() } }
addSource(recordsRepository)
}
錯誤添加源addSource()
None of the following functions can be called with the arguments supplied:
@MainThread public final fun <S : Any!> addSource(@NonNull p0: LiveData<List<RecordItem>!>, @NonNull p1: (List<RecordItem>!) -> Unit): Unit defined in androidx.lifecycle.MediatorLiveData
@MainThread public open fun <S : Any!> addSource(@NonNull p0: LiveData<List<RecordItem>!>, @NonNull p1: Observer<in List<RecordItem>!>): Unit defined in androidx.lifecycle.MediatorLiveData
您的LiveData
對象應該是val
。 使用MutableLiveData
/ MediatorLiveData
和setValue
或postValue
改變值LiveData
class RecordListViewModel @Inject constructor(val repository: RecordRepository): ViewModel() {
private val allRecords = MediatorLiveData<List<RecordItem>>().apply {
val recordsLiveData = repository.getAllRecording().map { records -> records.map { it.toItem() } }
addSource(recordsLiveData) { records ->
value = records
}
}
fun getAllRecords() : LiveData<List<RecordItem>> {
return allRecords
}
fun handleSelectedItem(id : Int) {
Log.d("HandleSelectedItem", "Test1")
allRecords.value?.let { records ->
allRecords.value = records.map { it.copy(isSelected = it.id == id) }
}
}
}
然后 Repository 和 Dao 都應該返回 MutableLiveData
不,不應該。 在您的DAO
和Repository
使用LiveData
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.