数据绑定:如果属性不为null,则设置属性。

62

如果变量字段不为空,如何仅设置视图的某些属性?我不太理解...


例如:

<?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异常。我该如何仅在属性不为null时设置它呢?
附言:对于英语表述不好,感到抱歉。


正如@Khemraj所提到的,除非您想为可空情况设置一些特殊值,否则无需检查值的可空性。数据绑定表达式可以免费为您提供此功能。 - HiddenDroid
3个回答

193

通常情况下,数据绑定通过检查空指针异常并分配默认值(例如null),即使在您的示例中item本身为null也是如此。

但对于项目属性的空值检查,这是一个基本示例:

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

或者使用 "Null Coalescing Operator"。null 合并运算符 (??) 如果左操作数不是 null,则选择左操作数;如果左操作数为 null,则选择右操作数。

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

请注意,titlegetTitle并不重要。


1
很抱歉,我不确定你在这里想要什么。你是想在新值为空时保留先前的值吗?我不确定通过数据绑定来实现这一点是否是一个好方法。我猜可以使用新的双向数据绑定,通过在某个对象的值不为空时设置(可能是其他对象的)值来实现。但我怀疑这不是一个好的做法。 - Mattias Isegran Bergander
1
一种方法是将使用的变量拆分,每个属性都有一个变量。类型为字符串而不是项目。 - Mattias Isegran Bergander
3
应该能正常工作,并且也可以使用数据绑定来控制可见性!android:visibility="@{item.drawable == null ? View.INVISIBLE : View.VISIBLE}" 但是你需要在data元素中添加一个View的引入:<import type="android.view.View"/> - Mattias Isegran Bergander
当我在ImageView中设置空的可绘制对象时,它会报错。我说过我的适配器可以为一个项目使用此图标,而不使用第二个项目。抱歉,也许我无法用英语清楚地解释它。我喜欢将值分开以便我可以单独控制可见性和可绘制对象。 - Denis Sologub
1
Aah,ImageView 不喜欢 null。建议在 null 上将其设置为一个空的或透明的 drawable。 - Mattias Isegran Bergander
显示剩余4条评论

25

数据绑定不需要检查 null 值,它将由绑定类处理。

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

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

上述两个示例是相同的。这里@string/male是默认值,当item.gendernull时。


7
我尝试过类似这样的东西。
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接