简体   繁体   中英

Android Data Binding mCallback is always null

I've been experimenting with the Android data binding library, folllowing the google developer guide. But even with following their code exactly notifyPropertyChanged() never works.

mCallbacks in the BaseObservable is always null. I've debugged the code as the binding is set up and addOnPropertyChangedCallback is called and mCallbacks is set, but for some reason this reference has been lost by the time you get round to calling notifyPropertyChanged() .

There's probably something I'm missing so any help would be much appreciated!

Code:

public class TestActivity extends AppCompatActivity {

TestModel mTestModel;
ActivityTestBinding mBinding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBinding = DataBindingUtil.setContentView(this, R.layout.activity_test);
    mTestModel = new TestModel("Test", "User");
    mBinding.setTestModel(mTestModel);

    mBinding.ratingBtnUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mTestModel.setFirstName("New");
            mTestModel.setLastName("WOOOOORKS");
            mBinding.notifyPropertyChanged(BR.firstName);
        }
    });
}}


public class TestModel extends BaseObservable {

private String firstName;
private String lastName;

public TestModel(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

@Bindable
public String getFirstName() {
    return this.firstName;
}

@Bindable
public String getLastName() {
    return this.lastName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}}

Layout:

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    >


    <data>
        <variable
            name="testModel"
            type="com.boundless.happymeter.model.TestModel"
            />
    </data>


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

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

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

        <Button
            android:id="@+id/rating_btn_update"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:text="Update"
            />

    </LinearLayout>


</layout>

Note: It is possible to force the binding by using mBinding.invalidateAll() - but this is quite an ugly soloution

you can use executePendingBindings() , it will execute all the binding which are changed and need to update.

binding.executePendingBindings();

Other solution is that you can use notifyPropertyChanged(); in all setter methods

public void setFirstName(String firstName) {
    this.firstName = firstName;
    notifyPropertyChanged(BR.firstName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM