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
<?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.
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.
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:
Start the Android application by choosing RunRun or by pressing Ctrl+F11, and you should see a screen similar to the one in Figure 11-2.
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:
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.
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:
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.
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:
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.
3.139.239.41