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’s bound to the result of a database query. First, we need to make the MainActivity class extend ListActivity instead of Activity:

eventsv2/src/main/java/org/example/events/MainActivity.java
 
import​ android.app.ListActivity;
 
// ...
 
public​ ​class​ MainActivity ​extends​ ListActivity {
 
// ...
 
}

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

eventsv2/src/main/java/org/example/events/MainActivity.java
 
import​ android.widget.SimpleCursorAdapter;
 
// ...
 
private​ ​static​ ​int​​[]​ TO = { R.id.rowid, ...

Get Hello, Android, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.