我正在尝试使用LiveData实现一个DB观察器,如中所述
android documentation
.
只要我在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>â
有人已经有这个问题了吗?
这是我的代码:
视图模型:
class ProfileViewModel: ViewModel() {
object FirstName: MutableLiveData<String>()
fun getCurrentName(): LiveData<String> {
return FirstName
}
}
碎片
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
}
}