Completing Your List Fragment
The ListFragment
class displays a list of items by binding to a data source such as an array or a cursor, and it exposes callback methods 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’s 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; for example, reminder_row.xml
. Inside this view, type the following code:
<?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 need when loading the list with data.
The ListFragment
requires that an adapter fill the contents of the list view. Various adapters are available, but because you don’t yet have a data store (built with an SQLite database in Chapter 12), you can temporarily create fake data so that you can see the list in action. In the following section, you add fake data, so you can set the ListFragment
’s adapter with a call to setListAdapter()
.
Getting stubby with fake data
Add the following field and method to your ReminderListFragment
class:
private ListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] items = new String[] { “Foo”, “Bar”, “Fizz”, “Bin” }; →7
mAdapter = new ArrayAdapter<String>(getActivity(), →9
R.layout.reminder_row, R.id.text1, items);
setListAdapter(mAdapter); →11
}
Here’s a brief explanation of the code:
→7 An array of string items that will eventually be displayed in the list.
→9 The creation of 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 is using Java generics, which allow you to specify the type of object that the ArrayAdapter
will work with. The constructor of the ArrayAdapter
contains these elements:
• getActivity()
: The current context. (Because the activity is an implementation of the Context
class, you 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
.
→11 The call to setListAdapter()
that informs the ListFragment
how to fill the ListView
. In this case, you’re using the ArrayAdapter
, created on line 4, to load the ListView
.
Start the Android application by choosing Run⇒Run or by pressing Ctrl+F11. The screen you see should look similar to Figure 9-3.
The previous code and example illustrate how to use a static data source for the ListFragment
. In Chapter 12, you replace this code with code that will load the data from an SQLite database.
Figure 9-3: The Task Reminder running with fake data.
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 briefly taps a view, such as a button.
Long-click: The user taps on a button and holds it for an extra moment.
Every view and activity can intercept these events via various methods. In the following sections, you respond to each type of event in a List Fragment
. In Chapter 11, you set up the app to respond to Button
click events.
Short clicks
The ListFragment
in Android does a lot of the event-handling heavy lifting for you (which is good because programming shouldn’t be a physical exercise).
After the onCreate()
method in ReminderListFragment
, type this method:
@Override
public 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’s provided by the ListFragment
. 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. When an item is clicked in this list, an intent opens the ReminderEditActivity
to allow the user to edit the item, as shown in the section “Starting new activities with intents,” later in this chapter.
Long clicks
A long-click (or long-press) occurs whenever a user presses a view for an extended period. To handle the list item’s long-click event in a ListFragment
, add the following line of code at the end of the onView Created()
method in ReminderListFragment
:
registerForContextMenu(getListView());
The outer method, registerForContextMenu()
, is responsible for registering a context menu to be shown for a given view. Multiple views can show a context menu; it isn’t limited to a single view. Every list item is therefore eligible to create a context menu. The registerForContextMenu()
accepts a View
object as a parameter that the ListFragment
should register as eligible for the context menu creation. The inner method, getList View()
, returns a ListView
object that’s used for the registration. The call, getListView()
, is a member of the ListFragment
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 it 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 ReminderListFragment
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’s being built.
v
: The view for which the context is being built (the view that the user long-clicked).
menuInfo
: Extra information about the item for which the context menu should be shown. (It can vary depending on the type of view in the v
parameter.)
Inside this method, you can modify the menu that’s presented to the user. For example, when a user long-presses an item in the task list, she should be allowed to delete it. Therefore, present her with the Delete option on a context menu (as described in Chapter 10).