I have searched for some similar questions before posting - however I have a general question when it comes to Android and data binding (and the other answers I check did not really get me much further...). Assume you have a class Vehicle:
public class Vehicle { private Owner owner; private String brand; //getter and setter for above attributes... }
and here is the Owner class ....
public class Owner { private String name; }
Now - I was just recently looking into MVVM (ModelView-ViewModel) pattern as employed by Microsofts WPF. Which got me wondering: Assuming I would want to bind the name property of my owner object which is a child of the Vehicle object - would there be some standard way in Android to achieve this? Also presuming, that I might have to validate input before I can have the Model updated?
I was imagining the following components (assuming MVVM):
Now when I add my EditTexts, TextViews and so on to the view, I want them to bind to certain the properties of my context object (Vehicle in this case) ... Mhhh if my question is not clear or you need further informatio do let me know :) thanks in advance.
P.s. I think people familiar with WPF might now what I mean? I myself just read about WPF and found it's a nice way to handle stuff.
P.P.s I am aware of the android binding project but I was wondering if there is a sort of build-in approach in Android or maybe some convention someone is following :) this really is more of a binding-theory question I guess ...
Data binding for events via listener bindings and method references. Events may be bound to handler methods directly, similar to the way android:onClick can be assigned to a method in the activity. Event attribute names are governed by the name of the listener method with a few exceptions. For example, View.
Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.
Up until now, we've used Data Binding to update the View from the ViewModel. LiveData is a handy data holder that acts as a container over the data to be passed. The best thing about LiveData is that it is lifecycle aware. So if you are in the background, the UI won't try to update.
View binding and data binding both generate binding classes that you can use to reference views directly. However, view binding is intended to handle simpler use cases and provides the following benefits over data binding: Faster compilation: View binding requires no annotation processing, so compile times are faster.
Google has launched its native Data Binding Library!
https://developer.android.com/tools/data-binding/guide.html
Which lets you write your views as such
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.firstName}"/>
But its event wiring mechanism at this time (rc1 version) can't bind to methods on a class extending from Observable (an exception is thrown when compiling; this is a known bug which will be solved).
And sure the lack of two-way binding (which is not expected to be on the first release) is a big drawback.
Anyway, here's a good sample project to play around with https://github.com/saleeh93/data-binding-samples
There is nothing "baked" into the Android SDK which provides equivalent databinding functionality found in MS WPF. Google is providing a lower level interface for the various devices running Android. To date, higher level application frameworks have not emerged from the Android development community. (IMHO, it would take a monster company like Google to create such a thing, given all the different constraints on a framework for all the various Android devices.)
You could create a set of databinding classes in your own application to handle your needs for the MVVM pattern. The "Databinders" would set the relevant event handlers on Android View objects and run some appropriate method on your ViewModel objects in response. They would also translate change events (that you define) on the ViewModel into the appropriate property assignments on the View objects.
Whether that turns out to be worthwhile for your application is a judgment call. My guess is that it would require more effort to write the Databinder classes than just to hook the lower level View event handlers directly. Frameworks are useful primarily when you didn't have to write them yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With