我正在学习kotlin和android的数据库。我能运行数据库功能。当我使用“可观测”时,我得到了BR.property的未解决的参考
这是我的model class:
data class FruitModel(var fruitImage: String?, var fruitName: String?) : BaseObservable() {
var imageUrl: String? = fruitImage
get() = field
set(value) {
field = value
notifyPropertyChanged(BR.imageUrl)
}
var nameValue: String? = fruitName
get() = field
set(value) {
field = value
notifyPropertyChanged(BR.fruitModel)
}}
我可以得到BR.fruitModel而不是以上两种。这是我的xml
<data>
<variable name="onClickItem"
type="com.wings.kotlintest1.interfaces.FruitAdapterInterface"/>
<variable name="fruitModel"
type="com.wings.kotlintest1.model.FruitModel"/>
<variable name="position"
type="int"/>
</data>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="5dp"
android:onClick="@{() -> onClickItem.onClickItemListener(position)}">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ivFruitImage"
android:layout_width="50dp"
android:layout_height="50dp"
app:loadImageWithGlide="@{fruitModel.fruitImage}"/>
<TextView
android:id="@+id/tvFruitName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:gravity="center_vertical"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:text="@{fruitModel.fruitName}"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
BR类没有生成属性的原因是什么?我做错什么了吗?
发布于 2019-06-24 10:14:45
我认为需要将@Bindable属性添加到这些字段的get()中。请参阅https://developer.android.com/topic/libraries/data-binding/observability
发布于 2019-06-24 10:16:24
我认为你需要使用@get:Bindable
data class FruitModel(var fruitImage: String?, var fruitName: String?) : BaseObservable() {
@get:Bindable
var imageUrl: String? = fruitImage
get() = field
set(value) {
field = value
notifyPropertyChanged(BR.imageUrl) // **unresolved reference : BR.imageUrl**
}
@get:Bindable
var nameValue: String? = fruitName
get() = field
set(value) {
field = value
notifyPropertyChanged(BR.nameValue) // **unresolved reference : BR.nameValue**
}
}发布于 2019-06-24 10:11:54
您的完整布局必须在<layout> ... </layout>内部。此外,尝试清理和建立您的项目。您还可以在生成窗口下的堆栈跟踪中跟踪错误的行。
https://stackoverflow.com/questions/56733924
复制相似问题