【发布时间】:2019-03-23 21:42:16
【问题描述】:
我正在使用 LiveData 和 ViewModel 使用 Android 架构组件实现双向数据绑定,但是当我构建项目时它给出了
error: cannot find symbol
import package.[layout_name]BindingImpl;
在DataBinderMapperImpl.java
我关注了官方文档并查看了 SO 以寻求答案,但他们都没有可行的解决方案。
layout.xml
<data>
<import type="package.ViewModel" /> // this line was added from an answer but didn't work
<variable
name="model"
type="package.ViewModel"/>
</data>
// an input field I want to bind data with
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={model.email}" // if I remove this line, builds fine
android:hint="@string/prompt_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true" />
从AndroidViewModel 扩展我的ViewModel 而不是文档中提到的BaseObservable
ViewModel.kt
private val email: MutableLiveData<String> by lazy { MutableLiveData<String>() }
@Bindable // tried to change the return type to String, still no luck
fun getEmail(): LiveData<String> {
return email
}
fun setEmail(email: String) {
this.email.value = email
}
这就是我将ViewModel 与View 绑定的方式
Activity.kt
binding.model = ViewModelProviders.of(this, ViewModelProvider.AndroidViewModelFactory
.getInstance(application))
.get(LoginViewModel::class.java)
我错过了什么?已经包含了所有数据绑定前的东西,如果我用data class 替换了layout 中的ViewModel 并尝试从中获取数据,那效果很好,但是@{} 在layout 中
编辑
好的,所以当我将email 公开为public 时,它可以编译和绑定工作,但我无法使其设置器和获取器public,我的意思是当我尝试从它的获取器和设置器中公开它时,IDE说这些已经是privatefunctions,不能是overriden?
如何让这个属性通过functions 暴露?
【问题讨论】:
标签: android android-databinding android-architecture-components android-jetpack