Reputation: 2060
I am learning kotlin and databinding for android. I am able to run function of databinding. While I am working with Observable, I am getting unresolve reference for BR.property
here is my 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)
}
}
I am able to get BR.fruitModel
instead of above two. Here is my 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>
what is reason that BR class is not generating properties? Am I doing something wrong?
Upvotes: 0
Views: 502
Reputation: 15313
I think you need to use @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**
}
}
Upvotes: 2
Reputation: 443
I think you need to add the @Bindable property to the get() of those fields. See https://developer.android.com/topic/libraries/data-binding/observability
Upvotes: 1
Reputation: 185
Your full layout must be inside <layout> ... </layout>
.
Also, try to clean and build your project.
You can also trace your wrong line in stacktrace under build window.
Upvotes: 0