如何修复在 Android 中使用双向数据绑定时找不到符号 view-model-name-BindingImpl
How to fix cannot find symbol view-model-name-BindingImpl when using two-way databinding in Android
我正在使用 LiveData
和 ViewModel
通过 Android 架构组件实现双向数据绑定,但是当我构建项目时它给出
error: cannot find symbol
import package.[layout_name]BindingImpl;
在DataBinderMapperImpl.java
我按照官方文档查看了 SO 的答案,但 none 其中有可行的解决方案。
已经试过this one and 一个
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
时,它可以编译和绑定,但我无法使其 setter 和 getter public
,我的意思是,当我试图从它的 getter 和 setter 中公开它时,IDE 说这些已经是 private
fun
动作,不能 override
n?
如何通过 fun
操作使此 属性 公开?
你可以直接使用get()
方法来使用特定变量的getter变量(也适用于 setter,如 set(value)
),如下所示:
@get:Bindable // We make getter method bindable for data-binding
val email = MutableLiveData<String>()
get() { // Try to provide getter method like this
return field as LiveData<String>
}
set(data) { // Try to provide setter method like this
if(field.value != data.value) // To avoid infinite loop
field.value = data.value
}
在 Reddit 上找到了答案。
要使双向数据绑定起作用,您必须公开您的字段,它们应该 MutableLiveData
类似于
val email = MutableLiveData<String>()
因为 kotlin
已经有 get
和 set
属性,它们将被 Binding
类 用于字段
我遇到了同样的问题,none 此处介绍的解决方案有效。
就我而言,问题是因为我有一个 Double 属性.
我将其更改为字符串并且有效。
我正在使用 LiveData
和 ViewModel
通过 Android 架构组件实现双向数据绑定,但是当我构建项目时它给出
error: cannot find symbol
import package.[layout_name]BindingImpl;
在DataBinderMapperImpl.java
我按照官方文档查看了 SO 的答案,但 none 其中有可行的解决方案。
已经试过this one and
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
时,它可以编译和绑定,但我无法使其 setter 和 getter public
,我的意思是,当我试图从它的 getter 和 setter 中公开它时,IDE 说这些已经是 private
fun
动作,不能 override
n?
如何通过 fun
操作使此 属性 公开?
你可以直接使用get()
方法来使用特定变量的getter变量(也适用于 setter,如 set(value)
),如下所示:
@get:Bindable // We make getter method bindable for data-binding
val email = MutableLiveData<String>()
get() { // Try to provide getter method like this
return field as LiveData<String>
}
set(data) { // Try to provide setter method like this
if(field.value != data.value) // To avoid infinite loop
field.value = data.value
}
在 Reddit 上找到了答案。
要使双向数据绑定起作用,您必须公开您的字段,它们应该 MutableLiveData
类似于
val email = MutableLiveData<String>()
因为 kotlin
已经有 get
和 set
属性,它们将被 Binding
类 用于字段
我遇到了同样的问题,none 此处介绍的解决方案有效。 就我而言,问题是因为我有一个 Double 属性.
我将其更改为字符串并且有效。