我的 MediatorLiveData 没有更新
My MediatorLiveData is not getting updated
我不知道为什么我的 mediatorLiveData 没有更新?我还在 Activity 文件中设置了观察者。我想做的是
- 单击一个按钮,将实时数据源添加到我的 mediatorLivedata
- 另一个按钮将不断更改实时数据,以便我的调解器实时数据应该更新(changeLiveData1 是此处的按钮)
我按照它似乎不起作用的顺序进行操作。我也在构造调用期间初始化了我的第一点。还是一样的问题。。
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var viewModel: MainViewModel
override
fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
viewModel.mediatorLiveData.observe(this, Observer {
text_view_content.text = it
})
livedata1.setOnClickListener {
viewModel.changeLiveData1()
}
livedata2.setOnClickListener {
viewModel.changeLiveData2()
}
add_source.setOnClickListener {
viewModel.addSourceLivedata1()
}
}
}
MainViewModel.kt
class 主视图模型:ViewModel() {
val mediatorLiveData: MediatorLiveData<String>
get() = MediatorLiveData()
val _livedata1: MutableLiveData<String>
get() = MutableLiveData<String>()
var change = 0
fun changeLiveData1() {
change++
_livedata1.value = "chnaged lived data...$change"
}
fun changeLiveData2() {
}
fun addSourceLivedata1() {
var count = 0
mediatorLiveData.addSource(_livedata1) {
count++
Log.d("MainView", "$count is ")
if (count > 5) {
mediatorLiveData.value = "changed from adding source... $count"
} else {
mediatorLiveData.value = "count is less than 5"
Log.d("MainView", "count is $count")
}
}
}
}
您每次访问变量时都会创建一个新实例
val mediatorLiveData: MediatorLiveData<String>
get() = MediatorLiveData()
val _livedata1: MutableLiveData<String>
get() = MutableLiveData<String>()
改为
val mediatorLiveData: MediatorLiveData<String> = MediatorLiveData()
val _livedata1: MutableLiveData<String> = MutableLiveData<String>()
我不知道为什么我的 mediatorLiveData 没有更新?我还在 Activity 文件中设置了观察者。我想做的是
- 单击一个按钮,将实时数据源添加到我的 mediatorLivedata
- 另一个按钮将不断更改实时数据,以便我的调解器实时数据应该更新(changeLiveData1 是此处的按钮)
我按照它似乎不起作用的顺序进行操作。我也在构造调用期间初始化了我的第一点。还是一样的问题。。 MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var viewModel: MainViewModel
override
fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
viewModel.mediatorLiveData.observe(this, Observer {
text_view_content.text = it
})
livedata1.setOnClickListener {
viewModel.changeLiveData1()
}
livedata2.setOnClickListener {
viewModel.changeLiveData2()
}
add_source.setOnClickListener {
viewModel.addSourceLivedata1()
}
}
}
MainViewModel.kt class 主视图模型:ViewModel() {
val mediatorLiveData: MediatorLiveData<String>
get() = MediatorLiveData()
val _livedata1: MutableLiveData<String>
get() = MutableLiveData<String>()
var change = 0
fun changeLiveData1() {
change++
_livedata1.value = "chnaged lived data...$change"
}
fun changeLiveData2() {
}
fun addSourceLivedata1() {
var count = 0
mediatorLiveData.addSource(_livedata1) {
count++
Log.d("MainView", "$count is ")
if (count > 5) {
mediatorLiveData.value = "changed from adding source... $count"
} else {
mediatorLiveData.value = "count is less than 5"
Log.d("MainView", "count is $count")
}
}
}
}
您每次访问变量时都会创建一个新实例
val mediatorLiveData: MediatorLiveData<String>
get() = MediatorLiveData()
val _livedata1: MutableLiveData<String>
get() = MutableLiveData<String>()
改为
val mediatorLiveData: MediatorLiveData<String> = MediatorLiveData()
val _livedata1: MutableLiveData<String> = MutableLiveData<String>()