Kann nicht verstehen … Wie kann man eine Ansichtseigenschaft nur dann festlegen, wenn das Variablenfeld nicht null ist?
Zum Beispiel
<?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="https://stackoverflow.com/questions/36227194/@{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>
Einige Felder von Item können null sein und ich werde nicht unnötig Methoden von Layoutansichten aufrufen. Und ich werde nicht bekommen NullPointerException
. Wie kann ich die Eigenschaft nur festlegen, wenn sie nicht null ist?
PS Entschuldigung für Englisch.
Wie von @Khemraj erwähnt, müssen Sie die Nullzulässigkeit von Werten nicht überprüfen, es sei denn, Sie möchten einige spezielle Werte für den Nullable-Fall festlegen. Datenbindungsausdrücke stellen dies kostenlos für Sie bereit
– Versteckter Droid
27. April 2019 um 18:45 Uhr