Game Development Reference
In-Depth Information
Intent intent =
new
Intent(
this
, clazz);
startActivity(intent);
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
}
}
The package name we chose is
com.badlogic.androidgames
. The imports should also be
pretty self-explanatory; these are simply all the classes we are going to use in our code. Our
AndroidBasicsStarter
class derives from the
ListActivity
class—still nothing special. The field
tests
is a string array that holds the names of all of the test activities that our starter application
should display. Note that the names in that array are the exact Java class names of the activity
classes we are going to implement later on.
The next piece of code should be familiar; it's the
onCreate()
method that we have to implement
for each of our activities, and that will be called when the activity is created. Remember that we
must call the
onCreate()
method of the base class of our activity. It's the first thing we must do
in the
onCreate()
method of our own
Activity
implementation. If we don't, an exception will be
thrown and the activity will not be displayed.
With that out of the way, the next thing we do is call a method called
setListAdapter()
. This
method is provided to us by the
ListActivity
class we derived it from. It lets us specify the list
items we want the
ListActivity
class to display for us. These need to be passed to the method
in the form of a class instance that implements the
ListAdapter
interface. We use the convenient
ArrayAdapter
class to do this. The constructor of this class takes three arguments: the first is our
activity, the second we'll explain in the next paragraph, and the third is the array of items that the
ListActivity
should display. We happily specify the
tests
array we defined earlier for the third
argument, and that's all we need to do.
So what's this second argument to the
ArrayAdapter
constructor? To explain this, we'd have to
go through all the Android UI API stuff, which we are not going to use in this topic. So, instead
of wasting pages on something we are not going to need, we'll give you the quick-and-dirty
explanation: each item in the list is displayed via a
View
. The argument defines the layout of
each
View
, along with the type of each
View
. The value
android.R.layout.simple_list_item_1
is a predefined constant provided by the UI API for getting up and running quickly. It stands for
a standard list item
View
that will display text. Just as a quick refresher, a
View
is a UI widget
on Android, such as a button, a text field, or a slider. We introduced views in form of a
Button
instance while dissecting the
HelloWorldActivity
in Chapter 2.
If we start our activity with just this
onCreate()
method, we'll see something that looks like the
screen shown in Figure
4-2
.