Adding a List
You will note that we are not adding the actual restaurant to anything – r is
a local variable and so goes out of scope after onClick() returns. We will
address this shortcoming later in this exercise.
Step #3: Implement toString().............................................................
To simplify the creation of our ListView, we need to have our restaurant
class respond intelligently to toString(). That will be called on each
restaurant as it is displayed in our list.
For the purposes of this tutorial, we will simply use the name – later
tutorials will make the rows much more interesting and complex.
So, add a toString() implementation on restaurant like this:
public String toString() {
return(getName());
}
Recompile and ensure your application still builds.
Step #4: Add a ListView Widget..........................................................
Now comes the challenging part – adding the ListView to the layout.
The challenge is in getting the layout right. Right now, while we have only
the one screen to work with, we need to somehow squeeze in the list
without eliminating space for anything else. In fact, ideally, the list takes up
all the available space that is not being used by our current details form.
One way to achieve that is to use a RelativeLayout as the over-arching
layout for the screen. We anchor the details form to the bottom of the
screen, then have the list span the space from the top of the screen to the
top of the details form.