实时数据。无法分配给“值”:setter 受保护/*受保护并打包*/用于合成扩展
LiveData. Cannot assign to ‘value’: the setter is protected/*protected and package*/ for synthetic extension
我正在尝试按照 android documentation 中所述使用 LiveData 实现数据库观察器。
只要我在 Kotlin 中编程,我就会调整函数(最初用 Java 编写)。
在尝试保存数据时,我发现了这个问题。
Cannot assign to ‘value’: the setter is protected/*protected and package*/ for synthetic extension in ‘<library Grade: android.arch.livecycle:livedata-core-1.1.1>’
有人遇到过这个问题吗?
这是我的代码:
ViewModel:
class ProfileViewModel: ViewModel() {
object FirstName: MutableLiveData<String>()
fun getCurrentName(): LiveData<String> {
return FirstName
}
}
Fragment
class ProfileFragment{
private lateinit var model: ProfileViewModel
// this is called onViewCreated. inputFirstName is an Edittext.
override fun setUp() {
model = ViewModelProviders.of(this).get(ProfileViewModel::class.java)
val nameObserver = Observer<String> { firstName ->
inputFirstName.text = SpannableStringBuilder(firstName)
}
model.getCurrentName().observe(this, nameObserver)
}
fun saveProfileData() {
val firstName = inputFirstName.text.toString()
model.getCurrentName().value = firstName
}
}
正如@spkink 建议的那样:
替换
fun getCurrentName(): LiveData<String>
和
fun getCurrentName(): MutableLiveData<String>
错误是因为setValue(T value)
在LiveData中是protected
(所以不能调用)而在MutableLiveData
中是public
。
model.getCurrentName().value = firstName
请考虑在 ViewModel 中处理名称值更新可能是一个更清晰的解决方案,因为您避免了可变的 public 属性。此外,这里可能会导致更糟糕的效果,因为您有两个可变的 public 实时数据属性。例如:
ViewModel
// keep your MutableLiveData private, so it's easier to find where mutations might happen in your code
// I changed a little how the variable is declared, as the way below seems to more common nowadays
private var _firstName = MutableLiveData<String>()
// LiveData should remain public, being a read-only interface with other classes like your fragment
fun getCurrentName(): LiveData<String> {
return _firstName
}
/*
An alternative
val firstName = LiveData<String>
get() = _firstName
*/
fun setFirstName(firstName: String){
_firstName.value = firstName
}
Fragment
fun saveProfileData() {
val firstName = inputFirstName.text.toString()
// the value update will be handled inside the ViewModel, where the private mutable live data property is located
model.setFirstName(firstName)
}
我正在尝试按照 android documentation 中所述使用 LiveData 实现数据库观察器。
只要我在 Kotlin 中编程,我就会调整函数(最初用 Java 编写)。
在尝试保存数据时,我发现了这个问题。
Cannot assign to ‘value’: the setter is protected/*protected and package*/ for synthetic extension in ‘<library Grade: android.arch.livecycle:livedata-core-1.1.1>’
有人遇到过这个问题吗?
这是我的代码:
ViewModel:
class ProfileViewModel: ViewModel() {
object FirstName: MutableLiveData<String>()
fun getCurrentName(): LiveData<String> {
return FirstName
}
}
Fragment
class ProfileFragment{
private lateinit var model: ProfileViewModel
// this is called onViewCreated. inputFirstName is an Edittext.
override fun setUp() {
model = ViewModelProviders.of(this).get(ProfileViewModel::class.java)
val nameObserver = Observer<String> { firstName ->
inputFirstName.text = SpannableStringBuilder(firstName)
}
model.getCurrentName().observe(this, nameObserver)
}
fun saveProfileData() {
val firstName = inputFirstName.text.toString()
model.getCurrentName().value = firstName
}
}
正如@spkink 建议的那样:
替换
fun getCurrentName(): LiveData<String>
和
fun getCurrentName(): MutableLiveData<String>
错误是因为setValue(T value)
在LiveData中是protected
(所以不能调用)而在MutableLiveData
中是public
。
model.getCurrentName().value = firstName
请考虑在 ViewModel 中处理名称值更新可能是一个更清晰的解决方案,因为您避免了可变的 public 属性。此外,这里可能会导致更糟糕的效果,因为您有两个可变的 public 实时数据属性。例如:
ViewModel
// keep your MutableLiveData private, so it's easier to find where mutations might happen in your code
// I changed a little how the variable is declared, as the way below seems to more common nowadays
private var _firstName = MutableLiveData<String>()
// LiveData should remain public, being a read-only interface with other classes like your fragment
fun getCurrentName(): LiveData<String> {
return _firstName
}
/*
An alternative
val firstName = LiveData<String>
get() = _firstName
*/
fun setFirstName(firstName: String){
_firstName.value = firstName
}
Fragment
fun saveProfileData() {
val firstName = inputFirstName.text.toString()
// the value update will be handled inside the ViewModel, where the private mutable live data property is located
model.setFirstName(firstName)
}