首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Kotlin:使用可观测数据更新UI的未解析引用

Kotlin:使用可观测数据更新UI的未解析引用
EN

Stack Overflow用户
提问于 2019-06-24 09:45:44
回答 3查看 424关注 0票数 0

我正在学习kotlin和android的数据库。我能运行数据库功能。当我使用“可观测”时,我得到了BR.property的未解决的参考

这是我的model class

代码语言:javascript
复制
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

代码语言:javascript
复制
<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类没有生成属性的原因是什么?我做错什么了吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-06-24 10:14:45

我认为需要将@Bindable属性添加到这些字段的get()中。请参阅https://developer.android.com/topic/libraries/data-binding/observability

票数 1
EN

Stack Overflow用户

发布于 2019-06-24 10:16:24

我认为你需要使用@get:Bindable

代码语言:javascript
复制
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**
        }
}
票数 2
EN

Stack Overflow用户

发布于 2019-06-24 10:11:54

您的完整布局必须在<layout> ... </layout>内部。此外,尝试清理和建立您的项目。您还可以在生成窗口下的堆栈跟踪中跟踪错误的行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56733924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档