我是数据绑定(bind)的新手,这是我的代码,但我在构建时遇到了这个错误
class DatabindingViewModel :ViewModel() {
val currentFruitName:LiveData<String>
get() = FakeRepository.currentName
fun changeNameOnClick()=FakeRepository.changeRandomFoodName()
//two way LiveData
@Bindable
val editTextContext= MutableLiveData<String>()
private val _displayEditTexfContent=MutableLiveData<String>()
val displayEditTexfContent:LiveData<String>
get()=_displayEditTexfContent
fun onDisplayEtText(){
_displayEditTexfContent.value=editTextContext.value
}
这是我的 xml 布局代码:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="binding"
type="ir.persiandesigners.myapplication.databinding.viewmodel.DatabindingViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:text="@{binding.currentFruitName}"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:onClick="@{()->binding.changeNameOnClick()}"
android:text="Get Random Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@={binding.editTextContext}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="@{()->binding.onDisplayEtText()}"
android:text="Change Display Text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
这是我在 Activity 中的代码:
val viweModel= ViewModelProviders.of(this)
.get(DatabindingViewModel::class.java)
DataBindingUtil.setContentView<ActvitiyDatadindingBinding>(
this,R.layout.actvitiy_datadinding)
.apply {
this.setLifecycleOwner( this@DataBindingAct)
this.binding=viweModel
}
viweModel.editTextContext.observe(this, Observer{
Toast.makeText(this@DataBindingAct,it ,Toast.LENGTH_LONG).show()
})
当我想要构建项目并运行它时,我得到了这个错误:
e: error: Bindable must be on a member in an Observable class. DatabindingViewModel is not Observable
你能帮帮我吗?
我已经尝试清理项目、重建等等,但是我的代码中有一个我找不到的错误
你能帮帮我吗?
最佳答案
只需从 Viewmodel 中移除 @Bindable 注解,您的代码就会成功运行,错误也会得到解决。
只有在扩展 BaseObservable 类时访问 getter 方法时才应应用 @Bindable 注释,该类本身实现了用于观察数据的 Observable 接口(interface)。
Here LiveData itself is an observable data holder class
因此,您不需要实现@Bindable 和BaseObservable 类来观察数据。
文档:https://developer.android.com/reference/android/databinding/Bindable
我希望现在一切都清楚了..
关于android - Bindable 必须在 Observable 类的成员上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56543945/