1

I'm facing a weird problem with the databinding library in android. In my todo app I want to use two way databinding for the add task form. This is my model which gets putted into the view layout.

public class TaskFormModel extends BaseObservable {

private String title;
private String notes;
private String categoryUUID;

public Date dueDate;


public TaskFormModel() {
}

public TaskFormModel(String title, String notes, String categoryUUID, Date dueDate) {
    this.title = title;
    this.notes = notes;
    this.categoryUUID = categoryUUID;
    this.dueDate = dueDate;
}

/**
 * Resets the current object
 */
public void reset() {
    title = null;
    notes = null;
    categoryUUID = null;
    dueDate = null;

    notifyPropertyChanged(BR.title);
    notifyPropertyChanged(BR.notes);
    notifyPropertyChanged(BR.categoryUUID);
    notifyPropertyChanged(BR.dueDate);
    notifyPropertyChanged(BR.showError);
}

/**
 * Determines if an error should be shown or not
 *
 * @return True if title is not null, but is empty. False otherwise
 */
@Bindable
public boolean isShowError() {
    return title != null && title.trim().isEmpty();
}

@Bindable
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
    notifyPropertyChanged(BR.title);
    notifyPropertyChanged(BR.showError);
}
...

}

In my layout I reference the model as shown in the following snippet

<data>
    <variable
        name="taskFormModel"
        type="de.einfachmachen.ui.addtask.form.TaskFormModel" />
</data>

<android.support.constraint.ConstraintLayout
    android:id="@+id/task_overview_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <TextView
        android:id="@+id/headline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Headline"
        android:text="@string/headline_add_task"/>


    <android.support.design.widget.TextInputLayout
        android:id="@+id/task_title_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/EditText"
        app:errorEnabled="true"
        app:error='@{taskFormModel.showError ? @string/error_task_title_missing : null}'
        app:hintTextAppearance="@style/TextFloatLabelAppearance"
        app:errorTextAppearance="@style/TextErrorAppearance"
        android:layout_marginLeft="@dimen/spacing_large"
        android:layout_marginRight="@dimen/spacing_large"
        android:layout_marginBottom="@dimen/spacing_large"
        app:layout_constraintTop_toBottomOf="@id/headline">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/task_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/Text"
            android:inputType="textCapSentences"
            android:lines="1"
            android:text="@={taskFormModel.title}"
            android:hint="@string/form_hint_title"/>
    </android.support.design.widget.TextInputLayout>

    ...

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/spacing_big"
        android:backgroundTint="@color/white"
        android:src="@mipmap/ic_launcher"
        app:fabSize="normal"
        app:borderWidth="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:rippleColor="@color/ripple_grey"/>

</android.support.constraint.ConstraintLayout>

If the user clicks the FloatingActionButton the task gets stored in the database and the TaskFormModel gets resetted via its reset method. As you can see the reset method sets the class attributes to null and afterwards uses notifyPropertyChanged to update the view. Everything works fine so far.

But if I inspect the attributes of the TaskFormModel after calling the reset method, the values aren't set to null, they have the value of an empty string. And because of this the isShowError method returns true which leads to displaying a form error to user. That is a non-desired behaviour.

So isn't it possible to set a class attribute to null with databinding? Did anybody experienced the same behaviour and knows how to fix the issue? Thanks in advance.

p.s.: I also tried to set a complete new TaskFormModel to the binding with binding.setTaskFormModel(new TaskFormModel());. No unfortunatley there was no success.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
jennymo
  • 1,450
  • 1
  • 18
  • 43
  • When do you want to show error? It was unclear. – Юрій Мазуревич Apr 29 '18 at 14:58
  • I want to show the error in the following two cases. 1st: If the user presses the submit button but didn't typed anything into the title field. Or 2nd if the user entered something into the title field and decides to clear the field via deleting all the letters. – jennymo Apr 30 '18 at 06:33

0 Answers0