Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bindings Stopped working

Just suddenly my bindings for android stoped working, anything I build now, I just get this message.

Error:Execution failed for task ':app:compileDevDebugJavaWithJavac'.

java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Cannot find the getter for attribute 'android:text' with value type java.lang.String on android.widget.EditText. file:C:\path\to\layout\layout.xml loc:85:12 - 96:54 ****\ data binding error ****

What I have tried

First it was suggested the bindings won't compile if there are errors in my files, so removed all layout files where I used bindings up to one file layout.xml. There I have

 <EditText
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:text="@{model.name}" />

... and it works well, however if i add the two-way binding android:text="@={model.name}" It throws the previous error.

Next, I add

@InverseBindingAdapter(attribute = "android:text")
public static String captureEditTextValue(EditText view) {
    return view.getText().toString();
}

...then it throws new error.

Error:Execution failed for task ':app:compileDevDebugJavaWithJavac'.

java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Could not find event 'android:textAttrChanged' on View type 'android.widget.EditText' file:C:\Users\EdgeTech\AndroidStudioProjects\purse\purse-customer\app\src\main\res\layout\get_phone_layout.xml loc:85:12 - 96:54 ****\ data binding error ****

Went further, to refactor to this

 @InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged")
 public static String captureEditTextValue(EditText view) {
        return view.getText().toString();
 }

...still gives previous error.

My Setup

  • Android Studio: 2.3.3
  • Gradle Build Tools: 2.3.3
like image 313
oreofeolurin Avatar asked Sep 22 '17 01:09

oreofeolurin


2 Answers

Two way binding needs this type:

ObservableField<T>

for example: in viewModel.class

public ObservableField<String> productName = new ObservableField<>();

in layout.xml:

<EditText
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:text="@={viewModel.productName}" />
like image 83
namezhouyu Avatar answered Oct 21 '22 06:10

namezhouyu


Get rid of the InverseBindingAdapter as I do not believe it is necessary for a String.

Then, in your EditText XML tag, change android:text="@={model.name} --> android:text="@={`` + model.name}".

like image 28
Randall Arms Avatar answered Oct 21 '22 06:10

Randall Arms