Android Programming Tutorials

(Romina) #1
Menus and Messages

onSave) or have clicked on (in onListClick). You will need to change
all references to the old r variable to be current in these two objects.


  1. Add imports for android.view.MenuItem and android.widget.Toast.

  2. Add the following implementation of onOptionsItemSelected() to
    your LunchList class:


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.toast) {
String message="No restaurant selected";

if (current!=null) {
message=current.getNotes();
}

Toast.makeText(this, message, Toast.LENGTH_LONG).show();

return(true);
}

return(super.onOptionsItemSelected(item));
}

Note how we will either display "No restaurant selected" (if current is null)


or the restaurant's notes, depending on our current state.


You can now rebuild and reinstall the application. Enter and save a


restaurant, with notes, then choose the Raise Toast option menu item, and


you will briefly see your notes in a Toast:


62
Free download pdf