Data Binding

Data binding allows you to connect your model (data) to your view with just a few lines of code. To demonstrate data binding, we'll modify the Events example to use a ListView that is bound to the result of a database query. First, we need to make the Events class extend ListActivity instead of Activity:

Download Eventsv2/src/org/example/events/


public class Events extends ListActivity { // ...

Next, we need to change how the events are displayed in the Events. showEvents( ) method:

Download Eventsv2/src/org/example/events/

import android.widget.SimpleCursorAdapter;

private static int[] TO = {,,, }; private void showEvents(Cursor cursor) { // Set up data binding

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,

R.layout.item, cursor, FROM, TO); setListAdapter(adapter);

Notice this code is much smaller than before (two lines vs. ten). The first line creates a SimpleCursorAdapter for the Cursor, and the second line tells the ListActivity to use the new adapter. The adapter acts as a go-between, connecting the view with its data source.

If you recall, we first used an adapter in the Translate sample program (see Translate.setAdapters() in Section 7.4, Using Web Services, on page 147). In that example, we used an ArrayAdapter because the data source was an array defined in XML. For this one, we use a SimpleCur-sorAdapter because the data source is a Cursor object that came from a database query.

The constructor for SimpleCursorAdapter takes five parameters:

• context: A reference to the current Activity

• layout: A resource that defines the views for a single list item

• cursor: The data set cursor

• from: The list of column names where the data is coming from

• to: The list of views where the data is going to

The layout for a list item is defined in layout/item.xml. Note the definitions for the row ID, time, and title views that are referenced in the TO array.

Download Eventsv2/res/layout/item.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="" android:layout_width= "fill_parent" android:layout_height="fill_parent" android:orientation= "horizontal" android:padding= "10sp"> <TextView android:id="@+id/rowid" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/rowidcolon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=": "

android:layout_toRightOf="@id/rowid" /> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/rowidcolon" /> <TextView android:id="@+id/timecolon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=": "

android:layout_toRightOf="@id/time" /> <TextView android:id="@+id/title" android:layout_width= "fill_parent" android:layout_height="wrap_content" android:ellipsize="end" android:singleLine= "true" android:textStyle="italic" android:layout_toRightOf="@id/timecolon" /> </RelativeLayout>

This looks more complicated than it is. All we're doing is putting the ID, time, and title on one line with colons in between the fields. I added a little padding and formatting to make it look nice.

Finally, we need to change the layout for the activity itself in layout/ main.xml. Here's the new version:

Download Eventsv2/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android= "'"

android:layout_width= "fill_parent"


<!-- Note built-in ids for 'list' and 'empty' -->

<ListView android:id="@android:id/list" android:layout_width= "'wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@android:id/empty" android:layout_width= "'wrap_content" android:layout_height= "'wrap_content" android:text="@string/empty" /> </LinearLayout>

Because the activity extends ListActivity, Android looks for two special IDs in the layout file. If the list has items in it, the android:id/list view will be displayed; otherwise, the android:id/empty view will be displayed. So if there are no items, instead of a blank screen the user will see the message "No events!"

Here are the string resources we need:

Download Eventsv2/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="app_name">Events</string> <string name="empty">No events!</string> </resources>

For the final result, see Figure 9.3, on the following page. As an exercise for the reader, think about how you could enhance this application now that you have a real list to play with. For example, when the user selects an event, you could open a detail viewer, mail the event to technical support, or perhaps delete the selected event and all the ones below it from the database.

Figure 9.3: This version uses a ListActivity and data binding.

There's still one little problem with this example. No other application can add things to the events database or even look at them! For that, we'll need to use an Android ContentProvider.

0 0

Post a comment