CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus 223
The first part of the evaluation structure starts with the switch keyword and something to evaluate
on a case-by-case basis—in this case, that would be the MenuItem object named item. The switch
statement, which is shown in Figure 7-8, would be coded as follows:
switch( item.getItemId( ) ) { case by case statements go in here }
Figure 7-8. Add a switch statement to the inside of the onOptionsItemSelected( ) method to evaluate MenuItem
As you can see, we are again “nesting” Java constructs, with the MenuItem object named item
calling the .getItemId( ) method by using dot notation inside of the switch( ) evaluation value
parameter area. What this is evaluating is fairly obvious, as it is looking at each MenuItem object’s ID
value which comes into the method.
Inside of curly braces will go case statements, each of which will have an ID value and then a colon,
as follows:
case R.id.edit_galaxy: Java programming statements to process, these are exited by using a Java
break; statement
After all of the case statements, there is a return statement, which we will be changing to true
once we add case evaluation statements. As you can see in Figure 7-8, once you type in the case
keyword and the R.id. precursor, the Eclipse pop-up helper dialog will give you all of the ID values
you have defined in your XML definition files.
Double-click on the edit_galaxy : int option shown highlighted in blue in Figure 7-8 to insert this
particular ID for your edit_galaxy MenuItem object, for evaluation in this first case statement. Next,
we will add Java statements inside of the case statement for the edit_galaxy MenuItem which will
create, name, and load an Intent object, which will be used to launch an EditGalaxy.java Activity
subclass which will contain our RelativeLayout UI container and an interactive UI design allowing
users to edit and change the Galaxy object data field values.