CHAPTER 14: Android Content Providers: Providing Data to Applications 541
off of the Toast object to show the message on the user’s display screen, looks like the following
Java code, also shown in Figure 14-32:
Toast.makeText( this, rulerName, Toast.LENGTH_LONG ).show();
This passes your Context object, using a Java this keyword, along with what you want to Toast, the
rulerName String object, and the LENGTH_LONG duration constant, which is also accessed using
dot notation off of the Toast object (class). I clicked on the rulerName String object in Figure 1 4-3 2
to show the object instantiation (initial tan highlight) and usage (second grey highlight) tracking.
Eclipse will do this for you if you ask it to by clicking on any object declared in your Java code.
Now you are ready to test the List All Galaxy Rulers UI button in your ContactsGalaxy Activity
subclass using the Run As ➤ Android Application menu sequence, as shown in Figure 14-33.
Select the Contact Galaxy Rulers option from your OptionsMenu in the ActionBar Overflow (three
dots) icon and run your TableLayout.
As you can see on the left side of Figure 14-33, your TableLayout does a good job creating your
database access UI design, using only a half dozen lines of code. Click the List All Galaxy Rulers UI
button element and you will see the Galaxy Ruler names that are currently in the database appear on
the bottom of the screen in Toast broadcast message format. This can be seen on the right side of
the figure at the bottom.
Writing to a Database: addGalaxyViceroy( )
Now you are ready to go to the next level of database access and write new values into the Contacts
database.
This is more complex, as well as more advanced, because the WRITE operation can change
the database, and is thus termed a destructive database operation in the database industry.
Conversely, a database READ operation is inherently non-destructive, as the data is only read, and
does not change!
In this section of the chapter, you are going to create another custom method for your
ContactGalaxy activity subclass, this time to write new Viceroy colony rulers into the Galaxy
Ruler Contacts database, which you are creating here as an example of how to work with Android
ContentProvider databases using ContentResolver objects.
Just like you did in the previous section, change the .finish( ) method call in the milkyWayButton
onClick( ) event handler method to an addGalaxyViceroy( ) method call. This will force Eclipse to
create your bootstrap method structure for you. As shown in Figure 14-34, you will select the second
“Create method in type ContactGalaxy.”