Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Binding to Data


Binding to Data


With data binding, you can declare data objects within your layout file:


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



name="crime"
type="com.bignerdranch.android.criminalintent.Crime"/>

...

And then use values from those objects directly in your layout file by using the binding mustache
operator – @{}:


<CheckBox
android:id="@+id/list_item_crime_solved_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="@{crime.isSolved()}"
android:padding="4dp"/>


In an object diagram, that would look like Figure 20.7:


Figure 20.7  The ties that bind


Your goal right now is to put the sound names on their buttons. The most direct way to do that using
data binding is to bind directly to a Sound object in list_item_sound.xml, like Figure 20.8:


Figure 20.8  Direct hookup

Free download pdf