【问题标题】:what's the content of "com.example.my.app.Fields" in DataBinding example?DataBinding 示例中“com.example.my.app.Fields”的内容是什么?
【发布时间】:2019-03-21 15:20:04
【问题描述】:

当我学习 Android DataBiding 时,页面Work with observable data objects 告诉我如何使用 ObservableList。

示例代码为:

<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”的内容是什么?Fields是一个kotlin的“对象”?还是 Fields 是一个带有伴生对象的 kotlin 类?

这两种我都试过了,但是同样的dataBinding错误——“Couldn't find accessor 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
    }
}

【问题讨论】:

    标签: android observablelist


    【解决方案1】:

    今天,我解决了。

    这样的代码:

    // 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中的字段,所以,我们需要添加一个注解。

    【讨论】:

      猜你喜欢
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 2014-08-16
      • 2013-04-25
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多