I have error at build time when using the ternary operator (data binding)?

Please tell me how to fix the error. In editText use double-sided binding:

<EditText
android:id="@+id/note_title_edit"
android:layout_width="parent"
android:layout_height="wrap content"
android:hint="@string/enter_title"
android:inputType="text"
 android:text='@={!TextUtils.isEmpty(note.title) ? note.title : ""}' />

The build step I get this error:

Found data binding errors. ****/ data binding error ****msg:The expression ((TextUtilsIsEmptyNoteTitle1) ? (noteTitle) : ("")) cannot be inverted: The condition of a ternary operator must be constant: android.databinding.tool.writer.KCode@73312afc


Why this error occurs I do not understand. The title field of the note object is of type String. Thanks in advance for your help.

Full code snippet:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:clickable="true"
android:focusable="true"
tools:context=".view.fragment.EditNoteFragment">

<data>
 <import type="android.text.TextUtils" />
 <import type="android.view.View" />

<variable
name="note"
 type="com.codit.recycleviewbinding.model.Note" />
</data>

<android.support.constraint.ConstraintLayout
android:layout_width="parent"
android:layout_height="parent"
android:background="@android:color/background_light">

<ImageView
android:id="@+id/preview_image"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:scaleType="fitCenter"
 android:visibility="@{TextUtils.isEmpty(note.image) ? View.GONE : View.VISIBLE}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
 app:noteImageUri="@{note.image}" />

<android.support.design.widget.TextInputLayout
android:id="@+id/note_title_input"
style="@style/textInputStyle"
android:layout_width="0dp"
android:layout_height="wrap content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:errorEnabled="@{TextUtils.isEmpty(note.title)}"
app:hintEnabled="@{!TextUtils.isEmpty(note.title)}"
app:layout_constraintEnd_toStartOf="@+id/add_image_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/preview_image">

<EditText
android:id="@+id/note_title_edit"
android:layout_width="parent"
android:layout_height="wrap content"
android:hint="@string/enter_title"
android:inputType="text"
 android:text='@={!TextUtils.isEmpty(note.title) ? note.title : ""}' />
</android.support.design.widget.TextInputLayout>


<android.support.design.widget.TextInputLayout
android:id="@+id/note_text_input"
style="@style/textInputStyle"
android:layout_width="0dp"
android:layout_height="wrap content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:errorEnabled="@{TextUtils.isEmpty(note.text)}"
app:hintEnabled="@{!TextUtils.isEmpty(note.text)}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/note_title_input">

<EditText
android:id="@+id/note_text_edit"
android:layout_width="parent"
android:layout_height="wrap content"
android:hint="@string/enter_text"
android:inputType="textMultiLine"
 android:text='@={!TextUtils.isEmpty(note.text) ? note.text : ""}' />
</android.support.design.widget.TextInputLayout>

<ImageButton
android:id="@+id/add_image_button"
android:layout_width="wrap content"
android:layout_height="wrap content"
android:layout_marginEnd="8dp"
android:background="@android:color/white"
android:src="@android:drawable/ic_menu_camera"
android:tint="@color/colorPrimaryDark"
app:layout_constraintBottom_toBottomof="@+id/note_title_input"
app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintTop_toTopOf="@+id/note_title_input" />

<Button
android:id="@+id/add_note_button"
android:layout_width="wrap content"
android:layout_height="wrap content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:background="@drawable/button_style"
android:text="@string/save_note"
android:textColor="@android:color/white"
 android:visibility="@{TextUtils.isEmpty(note.text) ? View.VISIBLE : View.GONE}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/note_text_input" />

<Button
android:id="@+id/edit_note_button"
android:layout_width="wrap content"
android:layout_height="wrap content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:background="@drawable/button_style"
android:text="@string/edit_note"
 android:visibility="@{TextUtils.isEmpty(note.text) ? View.GONE : View.VISIBLE}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/note_text_input" />
</android.support.constraint.ConstraintLayout>
</layout>
March 19th 20 at 09:00
2 answers
March 19th 20 at 09:02
Solution
@ = is a two - way (so like is called) linking, i.e., a EditText'linked to the field and Vice versa, the value of the field associated with EditText'ohms. When EditText is changed, it is unclear where to assign value, what is it you and says "The expression ((TextUtilsIsEmptyNoteTitle1) ? (noteTitle) : ("")) cannot be inverted".
either remove @ or a ternary operator(it is not necessary if the make field @NonNull is on any ground would be better).
@ -> normal
=@ -> bilateral

so that @ remove should not - dudley.Murazik16 commented on March 19th 20 at 09:05
March 19th 20 at 09:04
!TextUtils.isEmpty(note.title) ? note.title : "" has no sense there, and so will be nothing if the string is null/empty. Defaultnya binding adapters null safety.

if note.title is ObservableField or LiveData

<EditText
android:id="@+id/note_title_edit"
android:layout_width="parent"
android:layout_height="wrap content"
android:hint="@string/enter_title"
android:inputType="text"
 android:text='@={note.title}' />

Find more questions by tags Android