Graphics Programs Reference
In-Depth Information
formatting for the ListView items, as well as to collect the options ( string-
array ) for display from the options.xml file (using the getStringArray
method). To initiate a response by clicking the items in the ListView , class Main
implements the interface OnItemClickListener .
The real action happens inside the inherited method onItemClick . This is a call-
back method that is invoked when we click an item in the ListView . This method
provides a lot of information about the clicked item. At this stage, we need to know
the position of the clicked item in the ListView . This information is stored inside
the third argument of the onItemClick method ( int arg2 ). Keeping in mind
that the first item in the list is at position 0, Listing 2-2 shows how to handle the
clicks.
Listing 2-2. GAME MENU/src/com/apress/android/gamemenu/Main.java
public void onItemClick(AdapterView<?> arg0, View
arg1, int arg2, long arg3) {
if (arg2 == 0) {
startActivity(new Intent(Main.this, Game.class));
}
else if (arg2 == 1) {
Dialog d = new Dialog(this);
d.setContentView(R.layout.highscore);
d.setTitle("High Score");
d.show();
}
else if (arg2 == 2) {
Dialog d = new Dialog(this);
d.setContentView(R.layout.editplayer);
d.setTitle("Edit Player");
d.show();
}
}
Note The GAME MENU application consists of default responses
for handling most of the clicked items in the ListView . If you
 
 
Search WWH ::




Custom Search