1

I have problem with Android binding library. When I'm using property changed '_all' everything works, but when I'm specifying field it don't work. My question is why ? :)

public class Person extends BaseObservable{
private String name;

@Bindable
public String getName(){
    return this.name;
}

//IT WORKS
public void setName(String name){
    this.name = name;
    notifyPropertyChanged(BR._all); //<- difference
}

//IT DONT WORK
public void setSurname(String name){
    this.name = name;
    notifyPropertyChanged(BR.name); //<- difference
}

And my xml file:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
    <variable
        name="person"
        type="com.myapp.Person" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{person.getName()}" />

</LinearLayout>
</layout>
yennsarah
  • 5,467
  • 2
  • 27
  • 48
IlIlIl
  • 115
  • 9

1 Answers1

0

The problem is that you are using the method getName() instead of the property name. You should bind it like this:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{person.name}" />

The reason that _all works is because data binding considers that the entire object is invalid and reevaluated methods then as well.

George Mount
  • 20,708
  • 2
  • 73
  • 61
  • Okay, I notice that, but what if I want to use getter ? Example for changing value -> return format("Your name is %s", name); The only way is to use notify all? – IlIlIl Feb 10 '17 at 08:58
  • In that specific case, you'll want to use string formatting: `@{@string/yourName(name)}`. But if you pass the argument, it will also invalidate the method, so you could use: `@{MyStringFormatter.yourName(user.name)}` – George Mount Feb 10 '17 at 15:17