首页 > 解决方案 > 如何在 kotlin 的布局文件中绑定来自内部数据类的变量?

问题描述

我正在尝试从内部数据类绑定变量的值。但是我无法访问内部数据类中的变量。我已经在内部给出了外部数据类<data> <<data>

请在下面找到数据类和 layout.xml 文件。

data class User( val name:String ,val property:List<Property>)
data class Property(val propertyName:String, val owners:List<Owner>)
data class Owner(val ownerName:String)
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
        
     <data>
         <variable
             name="user"
             type="com..User" />
                
     </data>
     <LinearLayout>
         <TextView
             android:id="@+id/owner_textview"
             [...]
             android:text="{}"/>
        
         </<LinearLayout>
</layout>

我希望将所有者的名称绑定在 Textview 中。当我尝试使用 访问时 @{user.property[0].owners[0].ownerName},它无法访问。怎么能访问一样?

标签: kotlindata-binding

解决方案


推荐阅读