I'm having some issue with implementing two way binding with an Integer data type.
public class User {
private String firstName;
private String lastName;
private int age;
public User() {}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data class="UserDataBinding">
<variable
name="user"
type="com.databinding.model.User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.firstName}" />
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.lastName}" />
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.age}" />
</LinearLayout>
</layout>
Unfortunately, it gives me the error
"Error:(52, 17) Cannot find the getter for attribute 'android:text' with value type java.lang.Integer on android.support.design.widget.TextInputEditText. "
If I change the attribute text to
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={Integer.toString(user.age)}" />
then I get the error
"Error:cannot generate view binders java.lang.NullPointerException"
Appreciate any help on this.
UPDATE: It seems there was another error right after the error mentioned above.
cannot generate view binders java.lang.NullPointerException
Not sure why its giving me NPE even though the app hasn't started yet.
Yep, we don't use <layout> ViewBinding, because on DataBinding we must add that layout tag to generate Binding class and it's different with ViewBinding which automatically generates all layout to Binding class.
ViewBinding vs DataBindingThe main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.
In one-way binding, the flow is one-directional. In a two-way binding, the flow is two-directional. This means that the flow of code is from ts file to Html file. This means that the flow of code is from ts file to Html file as well as from Html file to ts file.
Well, six months later but maybe i can help someone.
You can do this simple trick:
android:text="@={`` + mObject.someNumber}"
OBS.: You need at least Android Studio 2.3
android:text="@{String.valueOf(Integer)}"
Somehow I got this to work by using BindingAdapter and InverseBindingAdapter.
public class User {
private String firstName;
private String lastName;
private int age;
public User() {}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
@BindingAdapter("android:text")
public static void setText(TextView view, int value) {
view.setText(Integer.toString(value));
}
@InverseBindingAdapter(attribute = "android:text")
public static int getText(TextView view) {
return Integer.parseInt(view.getText().toString());
}
}
Hopefully this will help someone else as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With