ant*_*009 4 android kotlin android-livedata
Android 4.1.2
Kotlin 1.4.21
Run Code Online (Sandbox Code Playgroud)
我添加了以下实时数据,但是当涉及到删除时,它不会删除任何元素。
val selectedLiveData by lazy { MutableLiveData<List<Core>>() }
Run Code Online (Sandbox Code Playgroud)
我不想触发观察者,所以我不分配,value因为我只想从 liveData 列表中删除单个元素,并且仅在添加时触发。
没有以下工作
selectedLiveData.value?.toMutableList()?.apply {
removeAt(0)
}
selectedLiveData.value?.toMutableList()?.apply {
removeFirst()
}
selectedLiveData.value?.toMutableList()?.apply {
remove(Core)
}
Run Code Online (Sandbox Code Playgroud)
我像这样添加元素,然后分配值,以便此实时数据的观察者得到更新:
selectedLiveData.value = selectedLiveData.value?.toMutableList()?.apply {
add(core)
}
Run Code Online (Sandbox Code Playgroud)
你想要的是
val selectedLiveData = MutableLiveData<List<Core>>(emptyList())
Run Code Online (Sandbox Code Playgroud)
然后
selectedLiveData.value = selectedLiveData.value.toMutableList().apply {
removeAt(0)
}.toList()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3859 次 |
| 最近记录: |