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 reference when loading the list with data.

image The view you just added is actually provided out of the box in the Android system. If you look at the Android documentation for simple_list_item_1 under Android.R.layout and inspect it via the Android source control repository, you will see virtually the same XML definition.

The ListActivity requires that an adapter fill the contents of the list view. Various adapters are available, but because you have not built a data store yet (you build it with an SQLite database in Chapter 14), you should create fake data so that you can see the list in action. When you have the fake data, you should set the ListActivity's adapter with a call to setListAdapter(). You do this next.

Getting stubby with fake data

Inside the onCreate() method of the ReminderListActivity.java file, after the call to setContentView(), add the following code:

String[] items = new String[] { “Say Hi to Gina”, “Go Crazy”, “Make a Bazillion
               Dollars”, “Plan Vacation” };
                                                                              →1

ArrayAdapter<String> adapter =
   new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1,
                            items);                                           →4
setListAdapter(adapter);                                                      →5

A brief explanation of the code is as follows:

→1 This line creates an array of string items. These items eventually will be displayed in the list.
→4 This line creates a new ArrayAdapter of string types. An ArrayAdapter manages a ListView backed by an arbitrary number of arbitrary objects — in this case, a simple string array. This code uses Java generics, which allow the developer to specify the type of object that the ArrayAdapter will be working with. The constructor of the ArrayAdapter contains the following:
  • this: The current context (Because the activity is an implementation of the Context class, I can use the current instance as the context.)
  • R.layout.reminder_row: The row layout that should be used for each row in the ListView
  • R.id.text1: The ID of the TextView inside R.layout.reminder_row in which to place the values from the array
  • items: The array of strings to load into the ListView
→5 The call to setListAdapter() that informs the ListActivity how to fill the ListView. In this case, I am using the ArrayAdapter created on line 4 to load the ListView.

Start the Android application by choosing RunimageRun or by pressing Ctrl+F11, and you should see a screen similar to the one in Figure 11-2.

Figure 11-2: The Task Reminder running with fake/stubbed data.

image

Handling user click events

The items in the list expose click events that allow the user to interact with each item. Android View objects have two main types of click events:

  • Click: The user taps a view such as a button.
  • Long click: The user taps and holds his finger on a button for a few moments.

Each view or activity can intercept these events through various methods. In the following section, I show you how to respond to each type of event in a ListActivity. In Chapter 13, I demonstrate responding to Button click events.

Dealing with (regular) clicks

The ListActivity in Android does a lot of the event-handling heavy lifting for you — which is good because programming shouldn't be a physical exercise!

To handle a click in a ListActivity, then, just add the following method after the onCreate() method in the ReminderListActivity.java file:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
}

This code overrides the default implementation of onListItemClick() that is provided by the ListActivity. Now when a list item is clicked, this method is called and the following parameters are passed into the call:

  • l: The ListView where the click happened
  • v: The item that was clicked with the ListView
  • position: The position of the clicked item in the list
  • id: The row ID of the item that was clicked

Using these variables, you can determine which item was clicked and then perform an action based on that information. The plan is that when an item is clicked, an intent that opens the ReminderEditActivity gets fired, which allows users to edit the item, as shown in the section “Starting new activities with intents,” later in this chapter.

Dealing with long clicks

Long clicks, also known as long presses, occur when a user presses a view for an extended period of time. To handle the list item's long-click event in a ListActivity, add the following line of code at the end of the onCreate() method:

registerForContextMenu(getListView());

The outer method, registerForContextMenu(), is responsible for registering a context menu to be shown for a given view. (However, multiple views can show a context menu; it's not just limited to a single view.) Therefore, each list item is eligible to create a context menu. The registerForContextMenu() accepts a View object as a parameter that the ListActivity should register as eligible for the context menu creation. The inner method, getListView(), returns a ListView object that is used for the registration. The call, getListView(), is a member of the ListActivity class.

Now that you've registered the ListView to be eligible to create a context menu, you need to respond to the long-click event on any given item. When an item is long-clicked in the ListView, the registerForContextMenu() recognizes this and calls the onCreateContextMenu() method when the context menu is ready to be created. In this method, you set up your context menu.

At the end of the ReminderListActivity.java class file, type the following method:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo
               menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
}

This method is called with the following parameters:

  • menu: The context menu that is being built.
  • v: The view for which the context is being built (the view you long-clicked on).
  • menuInfo: Extra information about the item for which the context menu should be shown. This can vary depending on the type of view in the v parameter.

Inside this method, you can modify the menu that will be presented to the user. For example, when a user long-presses an item in the task list, you want to allow her to delete it. Therefore, you need to present her with a Delete context menu option. You add the Delete item to the context menu in Chapter 13.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.130.24