Creating Your First List Activity

In order for a user of your application to view his tasks, you must create a list of these tasks in Android. Lists in Android are handled by the ListView and ListActivity. In the section below, you create a list that houses the list of tasks the user has created by using the ListView and ListActivity. You also create a custom row that each one of the task items will use in the list to display itself.

The ListActivity class displays a list of items by binding to a data source, such as an array or cursor, and exposes callback methods that get executed when the user selects an item. However, to build a list of items to display in a list, you need to add a layout that defines what each row will look like.

A cursor provides random read and write access to the result set that is returned by a database query.

Add a new layout to the res/layout directory with a root element of TextView and give it a proper name for a row type of item — something like reminder_row.xml. Inside this view, type the code shown in Listing 11-4

Listing 11-4: The reminder_row.xml File

<?xml version=“1.0” encoding=“utf-8”?>
<TextView
    xmlns:android=“http://schemas.android.com/apk/res/android”
    android:id=“@+id/text1”                                          →4
    android:layout_width=“fill_parent”
    android:layout_height=“fill_parent”
    android:padding=“10dip”/>

This code simply defines a row in which text values can be placed with a padding of ten density-independent pixels. Line 4 defines the ID of the view that you will ...

Get Android™ Tablet Application Development For Dummies® 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.