2017-01-20

In the last example, you may have got an idea of what DataBinding is.

If not please check my tutorial here…

Simple DataBinding in Android Example.

Updating Objects using Observables.

In the last tutorial you may have noticed, if you update the object which is data binded, the change is not reflected in the UI right. Our requirement is whenever we change the object, I want to update the UI as well.

Our issue in the last post..

Here I am trying to update the user object which was actually databinded, it is not reflected in the UI.

So what do we do.



Solution

The solution is to extend the User Class with BaseObservable.

So our new User class will look like this.

Here observe that we have all the getters with @Bindable notation on top of it. In this way we are telling the DataBinder that whenever this object is changed, update the UI as well. So we need to change the setter to notify this change like below.

BR is automatically generated for you. Either you can specify the whole package name before it or Just BR.

Now Try again with the button Click Listener, When the Object is updated, the UI is updated as well.

No More extra setText needed to update the UI.



Here is the complete Activity Class.

MainActivity

Please try a Clean & Build if you are facing any issues

Source Code

You can find the full source code in my GitHub repository.

Get the source code.

More Similar Posts

Simple DataBinding in Android Example.

Failed to resolve: com.google.firebase:firebase Errors in…

A Simple Chat Application in Android Using GCM and Server…

Using GSON in Android – Simple Demo

What are static variables? Why it is used? What is…

Simple way to Animating Layout Changes in Android.

What is @property in iOS mean?

Solution for CoreData Multithreading problem.

Best coding practices in Android Part 1

Xcode 6 crashes when validating or submitting app archive.

Implementing Google Analytics in your Android app –…

Custom Transitions in ANDROID.

Contacts Demo in Swift 2 – Showing Contacts Picker,…

Best Coding Practices in Android – Part 2

Create PDF in iOS

CRUD Operations in SQLite using SQLiteOpenHelper –…

Using NSOperationQueue in iOS to do Serial/Concurrent…

Simple Core Data tutorial in iOS

Using Loaders in Android Using SimpleCursorAdapter in…

Error Handling in Swift

What are Generics and Why should we use Generics?

Account Authenticator Demo using AccountManager in Android

RecyclerView Simple Example in Android.

How will you create a static library in iOS?

Using “BackgroundFetch” in iOS to do task in the

Show more