mantc_sdr
mantc_sdr

Reputation: 491

DataBinding not updating when passing LiveData<?> instead of ViewModel

I am using DataBinding, and in my ViewModel I have a LiveData<MyItem> object which I want to bind to my layout. The item has one attribute like this:

@Bindable
public int getAmount() {
    return amount;
}

public void setAmount(int amount) {
    this.amount = amount;
    notifyPropertyChanged(BR.aMOUNT);
}

The thing is that if I indicate in the Fragment:

mDataBinding.setItem(mViewModel)

and in the layout I have:

<Button
...
android:enabled="@{viewModel.item.amount == 0}/>

it binds it and once an attribute amountt from Item changes, enables or disables a button. OK.

The problem becomes when, instead of passing the viewModel instance, I directly pass the Item instance:

mDataBinding.setItem(mViewModel.getItem().getValue())

and in layout:

<Button
...
android:enabled="@{item.amount == 0}/>

the problem then is that it enables/disables the button the first time correctly but it does not changes its state when item's amount attribute changes. Do you know why does it happen??

Upvotes: 0

Views: 661

Answers (1)

M&#252;cahid Kambur
M&#252;cahid Kambur

Reputation: 1678

Can you set binding.lifecycleOwner = this before the viewModel? Also if you use fragment use lifeCycleOwner instead of this

Upvotes: 1

Related Questions