数据绑定:如果不为空则设置 属性

Data binding: set property if it isn't null

无法理解...如何仅在变量字段不为空时设置一些 属性 视图?
例如

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="item"
            type="com.test.app.Item" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_margin="16dp"
            android:src="@{item.getDrawable()}"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginEnd="16dp"
            android:layout_marginLeft="72dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="72dp"
            android:layout_toLeftOf="@id/action"
            android:layout_toStartOf="@id/action"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textColor="@color/black_87"
                android:textSize="16sp"
                android:text="@{item.getTitle()}"/>

            <TextView
                android:id="@+id/text2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:autoLink="web|email"
                android:linksClickable="false"
                android:singleLine="true"
                android:textColor="@color/black_54"
                android:textSize="14sp"
                android:text="@{item.getSubtitle()}"/>


        </LinearLayout>

    </RelativeLayout>
</layout>

Item的某些字段可以为空,我不会不必要地调用布局视图的方法。而且我不会得到 NullPointerException。仅当 属性 不为空时如何设置?
P.S。对不起英语。

Well 数据绑定通常通过检查 NullPointerException 来避免 NullPointerException 并分配默认值(例如 null),即使 item 本身在您的示例中为 null。

但是对项目属性进行空值检查的基本示例:

android:text='@{item.title != null ? user.title : ""}'

或使用"Null Coalescing Operator"。空合并运算符 (??) 选择左侧操作数(如果它不为空)或右侧(如果它为空)。

android:text='@{item.title ?? ""}'

请注意 titlegetTitle 无关紧要。

Data binding does not need to check for null value, it will be handled by binding class.

如果您出于其他目的(例如设置默认值)需要检查 null,则可以这样使用。

android:text='@{item.gender != null ? item.gender : @string/male}'

android:text='@{item.gender ?? @string/male}'

以上两个例子都是一样的。这里@string/male是默认值,当item.gendernull.

我试过类似的方法。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

  <data>
     <import type="android.text.TextUtils"/>
     <variable
        name="mOrder"
        type="com.test.app.models.Order" />
  </data>
  <TextView
    android:id="@+id/mLblSource"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text='@{TextUtils.isEmpty(mOrder.order_id) ? "- -" : mOrder.order_id}'
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" />
</layout>