Two-way binding cannot resolve a setter for java.lang.String property
Asked Answered
U

3

9

I am playing with the two-way binding of the data binding API which was introduced in Android Studio 2.1 AFIK.

I get this interesting error:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:The expression address.street cannot cannot be inverted: Two-way binding cannot resolve a setter for java.lang.String property 'street'
file:/path/to/layout.xml
loc:34:37 - 34:50 ****\ data binding error ****

When I try to google that error I just find a 4 day old Japanese Twitter posting from a guy who is crying about it...

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/edit_hint_zip"
    android:text="@={address.zip}"
    tools:text="12345"/>

That address.zip is a String. I am guessing that the problem here is CharSequence vs. String as the return value of EditText.getText().

My idea was to defining it however this does not work for me:

@NonNull
@InverseBindingAdapter(attribute = "text")
public static String getText(EditText edit) {
    return edit.getText().toString();
}

What did I miss?

Unbound answered 30/4, 2016 at 19:24 Comment(0)
U
10

This bug is ugly as hell and properly a bug in the data binding API. The solution is to generate a setter and a getter. I came up fast with the idea to create a setter, but not to create a getter.

Here is now my simplified model:

public class Address {
    public String street;

    public void setStreet(String street) {
        this.street = street;
    }

    public String getStreet() {
        return street;
    }
}

As you may note the getter and setter are useless, but required for two way binding.

If you think that this is a bug of the API please star my bug report: Two-way binding required setters AND ALSO getters

Unbound answered 30/4, 2016 at 20:54 Comment(1)
It also works at kotlin for booleans like checkbox checked fieldMatrimony
R
33

If you are working with kotlin , make sure data class field used for two way binding is declared as var. If it is val unable to support two way binding

Retarder answered 30/8, 2019 at 14:29 Comment(3)
This worked for me, but cold you give additional information why data binding does not work on val propertiesTiti
In Kotlin Val means static. Var means non-static. In databinding, the variable values may be changed so must declare as var.Retarder
In Kotlin val doesn't mean static it means final. I think speaking from a two way binding perspective, it does make sense to have your class properties declared as var because, you do want their properties to change.Punak
U
10

This bug is ugly as hell and properly a bug in the data binding API. The solution is to generate a setter and a getter. I came up fast with the idea to create a setter, but not to create a getter.

Here is now my simplified model:

public class Address {
    public String street;

    public void setStreet(String street) {
        this.street = street;
    }

    public String getStreet() {
        return street;
    }
}

As you may note the getter and setter are useless, but required for two way binding.

If you think that this is a bug of the API please star my bug report: Two-way binding required setters AND ALSO getters

Unbound answered 30/4, 2016 at 20:54 Comment(1)
It also works at kotlin for booleans like checkbox checked fieldMatrimony
P
0

According to databinding offical repo https://android.googlesource.com/platform/frameworks/data-binding/ commit message, this bug has been fixed at Android Studio 2.2 preview 3.

Prittleprattle answered 16/6, 2016 at 9:4 Comment(3)
Can you link the commit you are talking about? I see there just one commit which was pushed 8 weeks ago, which seems not to been related.Unbound
Do you mean the commit 0137f2?Unbound
Yes, and I also tested on different versions of Android Studio.Prittleprattle

© 2022 - 2024 — McMap. All rights reserved.