Using POIDetailActivity
, users can choose to save new or existing POIs, or delete existing POIs. We need a way to accomplish these tasks from the user interface. We will use ActionBar
again and add two actions, Save
and Delete
.
The following listing shows what is needed for POIDetailMenu.xml
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/actionSave" android:icon="@drawable/ic_save" android:title="Save" android:showAsAction="ifRoom" /> <item android:id="@+id/actionDelete" android:icon="@drawable/ic_delete" android:title="Delete" android:showAsAction="ifRoom" /> </menu>
Note that each menu item has an icon specified. These icons can be found in the [assets location]drawable
folder.
The OnCreateOptionsMenu()
and OnOptionsItemSelected()
methods are also very similar to what we created in the previous chapter. The following code snippet is the modified version:
public override bool OnCreateOptionsMenu(IMenu menu) { MenuInflater.Inflate(Resource.Menu.POIDetailMenu, menu); return base.OnCreateOptionsMenu(menu); } public override bool OnOptionsItemSelected (IMenuItem item) { switch (item.ItemId) { case Resource.Id.actionSave: SavePOI (); return true; case Resource.Id.actionDelete: DeletePOI (); return true; default : return base.OnOptionsItemSelected(item); } }
In this case notice that we have added the methods SavePOI()
and DeletePOI()
, which get called to do all the work. This keeps the OnOptionsItemSelected()
method clean and concise.
One thing that's different in POIDetailView
is that we have a scenario where we need to disable an action. If a new POI is being created, the Delete
action should not be allowed. We get a chance to make these types of changes in the OnPrepareOptionsMenu()
method.
The following listing shows how to disable the Delete
action when a new POI is being entered:
public override bool OnPrepareOptionsMenu (IMenu menu) { base.OnPrepareOptionsMenu (menu); // disable delete for a new POI if (!_poi.Id.HasValue) { IMenuItem item = menu.FindItem (Resource.Id.actionDelete); item.SetEnabled (false); } return true; }
Notice that IMenu
provides a FindItem()
method that can be used to obtain a reference to a specific IMenuItem
, which in turn provides the SetEnabled()
method for enabling and disabling actions.
The SavePOI()
method was created so that we could avoid placing a lot of logic in the OnOptionsItemSelected()
method. The SavePOI()
methods needs to accomplish the following:
SavePOI()
on POIJsonService
.POIDetailActivity
activity by calling Finish()
.We will cover validation in an upcoming section and focus now on the remaining three items. The following listing shows what should be present in SavePOI()
:
protected void SavePOI() { _poi.Name = _nameEditText.Text; _poi.Description = _descrEditText.Text; _poi.Address = _addrEditText.Text; _poi.Latitude = Double.Parse(_latEditText.Text); _poi.Longitude = Double.Parse(_longEditText.Text); POIData.Service.SavePOI (_poi); Finish (); }
Like SavePOI()
, the DeletePOI()
method was created to simplify the logic in OnOptionsItemSelected()
. The DeletePOI()
method needs to accomplish the following:
DeletePOI
() on POIJsonService
.POIDetailActivity
activity by calling Finish()
.The following listing shows what should be present in the DeletePOI()
method:
protected void DeletePOI() { POIData.Service.DeletePOI (_poi); Finish (); }
You should now be able to run the app and add, change, and delete POIs.
18.191.186.72