Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data binding: set property if it isn't null

Cannot understand... How to set some property of view only if variable field isn't null?
For example

<?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> 

Some field of Item can be null and I won't call methods of layout views unnecessarily. And I won't get NullPointerException. How can I set property only if it isn't null?
P.S. Sorry for English.

like image 868
Шах Avatar asked Mar 25 '16 20:03

Шах


People also ask

How check data binding is null in Android?

Generated data binding code automatically checks for null values and avoid null pointer exceptions. For example, in the expression @{user.name} , if user is null, user.name is assigned its default value of null . If you reference user. age , where age is of type int , then data binding uses the default value of 0 .

Can I use both data binding and view binding?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.

Which is the correct way to reference bound data in the XML layout?

Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression.


1 Answers

Well data binding avoids NullPointerException in general by checking for it and assigns the default value (null for example) even if item itself is null in your example.

But a basic example for null checks for the item's properties:

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

Or use the "Null Coalescing Operator". The null coalescing operator (??) chooses the left operand if it is not null or the right if it is null.

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

Note that title or getTitle doesn't matter.

like image 106
Mattias Isegran Bergander Avatar answered Sep 29 '22 14:09

Mattias Isegran Bergander