数据绑定示例中"com.example.my.app.Fields"的内容是什么?



当我研究Android数据框时,该页面可与可观察的数据对象一起使用。

示例代码是:

<data>
    <import type="android.databinding.ObservableList"/>
    <import type="com.example.my.app.Fields"/>
    <variable name="user" type="ObservableList<Object>"/>
</data>
…
<TextView
    android:text='@{user[Fields.LAST_NAME]}'
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<TextView
    android:text='@{String.valueOf(1 + (Integer)user[Fields.AGE])}'
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

但是," com.example.my.app.fields"?字段的内容是什么?或字段是带有伴侣对象的Kotlin类?

我曾尝试过这两个,但是得到相同的数据列错误 - "找不到访问者xxx.fields.name"。

有人可以告诉我如何定义字段吗?非常感谢!

这是我的代码:

activity_temp.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="user"
            type="android.databinding.ObservableArrayList&lt;Object>" />
        <import type="com.databinding.ui.Fields" />
    </data>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="15dp">
        <!-- When i use  android:text="@{user[0]}" ,it's ok -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user[Fields.NAME]}"
            tools:text="NAME" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="@{user[1]}"
            tools:text="SEX" />
        <Button
            android:id="@+id/bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="Click Here" />
    </LinearLayout>
</layout>

tempactivity.kt - 一个.kt类文件

class TempActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityTempBinding = DataBindingUtil.setContentView(this, R.layout.activity_temp)
        val user = ObservableArrayList<Any>().apply {
            add("NAME: CnPeng")
            add("SEX:Man")
            add(17)
        }
        binding.user = user
        var clickCount = 0
        binding.bt.setOnClickListener {
            clickCount++
            user[Fields.NAME] = "NAME: CnPeng, ClickNum:$clickCount"
            user[Fields.SEX] = "SEX:Man, ClickNum:$clickCount"
            user[Fields.AGE] = clickCount
        }
    }
}

// Couldn't find accessor  xxx.Fields.NAME
//object Fields {
//    val NAME: Int = 0
//    val SEX: Int = 1
//    val AGE: Int = 2
//}
// Couldn't find accessor  xxx.Fields.NAME
class Fields {
    companion object {
        val NAME: Int = 0
        val SEX: Int = 1
        val AGE: Int = 2
    }
}

今天,我解决了。

这样的代码:

// It's ok
//object Fields {
//    @JvmField
//    val NAME: Int = 0
//    @JvmField
//    val SEX: Int = 1
//    @JvmField
//    val AGE: Int = 2
//}
// It's Ok
class Fields {
    companion object {
        @JvmField
        val NAME: Int = 0
        @JvmField
        val SEX: Int = 1
        @JvmField
        val AGE: Int = 2
    }
}

我在字段上方添加了@jvmfield。

因为编译时,数据列表库为.java类,所以.java文件会在.kt中调用字段,因此,我们需要添加注释。

最新更新