0

Today I've some question about mvvm and databinding on android,

I'm trying to bind object properties on view.

I've an Object (Model) with some properties, by example :

public String name;
public String title;
public int value;

I've a ViewModel with livedata like this :

MutableLiveData<Object> _obj = new MutableLiveData<>();
public LiveData<Object> obj = _obj;

And, at last, I've a view like this :

<layout>
    <data>
        <variable
            name="viewModel">
            type="com.sample.app.viewmodels.MainViewModel" />
    </data>
    <LinearLayout
        ... >
        <TextView
            android:text:="@{viewModel.obj.name}"
            .../>
    </LinearLayout>
</layout>

I saw that we can do that in a video from "Android Developers" about "LiveData" : https://youtu.be/OMcDk2_4LSk?t=102

She says that its possible in Android studio on 3.1+ versions. But this is not working for me.

C. MARTIN
  • 43
  • 12
  • Have you set the ViewModel in your Activity/Fragment binding? Check this [link](https://developer.android.com/topic/libraries/data-binding/architecture) – Sanlok Lee Jul 05 '19 at 20:17
  • Hi, yes, I use this : binding.setLifecycleOwner(this) – C. MARTIN Jul 05 '19 at 20:35
  • do you also set `binding.viewModel = viewModel`? Can you show the pare where you set these binding parameters – Sanlok Lee Jul 05 '19 at 20:59
  • Yes, I do this : `public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class); MainFragmentBinding binding = MainFragmentBinding.inflate(getLayoutInflater()); binding.setViewModel(viewModel); binding.setLifecycleOwner(this); return binding.getRoot(); }` – C. MARTIN Jul 05 '19 at 21:14
  • why you create a ```livedata``` from your ```MuteableLiveData```? why donsn't use the ```MuteableLiveData``` for binding? – mahdi shahbazi Jul 06 '19 at 06:11
  • Yes it's olso possible, and at the first time it was the case, but it's the same result. – C. MARTIN Jul 06 '19 at 08:13
  • Finally its working ! but the object fields are not provided by Android Studio. I do this manualy. Another thing does not work, the ui update, when I change a value in field I do a calculation and I'd like to update my textview when the values changes. – C. MARTIN Jul 06 '19 at 15:35
  • Someone can help me ? – C. MARTIN Jul 08 '19 at 16:59

1 Answers1

2

For this to work, your model class must extend BaseObservable class from databinding library. And you have to call notifyChange() on each setter method like this:

public class Object extends BaseObservable {
    public String name;
    public String title;
    public int value;

    public void setName(String name) {
        this.name = name;
        notifyChange();
    }

    public void setTitle(String title) {
        this.title = title;
        notifyChange();
    }

    public void setValue(int value) {
        this.value = value;
        notifyChange();
    }
}
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43