Search code examples
androidkotlinandroid-databinding

Kotlin: unresolved reference for Updating UI using Observables


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?


Solution

  • 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