Challenge Exercise: Delete Crime records criminalintent references currently do not support deleting existing crimes records, add menu items for Crimefragment, allow users to delete current record, click Delete menu item, Remember to call the finish method of the Crimefragment managed activity to fall back to the previous activity interface.
First, add a menu item for crime.
Create a layout file named Crimefragment_crime under the menu folder with the following code:
<?xml version="1.0"encoding="Utf-8"? ><menu xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:app="Http://schemas.android.com/apk/res-auto"> <Item Android:id="@+id/delete"Android:title="Delete"app:showasaction="Ifroom"/></menu>
After that, add the following code to the Crimefragment to show the menu items:
1 @Override 2 Public void Oncreateoptionsmenu (Menu menu, Menuinflater inflater) {3 Super . Oncreateoptionsmenu (menu, inflater); 4 inflater.inflate (r.menu.crimefragment_crime,menu); 5 }
To delete the crime record, we need to delete the selected entry in the list that holds the crime object. Add the following code to the Crimelab:
1 Public voidremovecrime (UUID id) {2Iterator<crime> it =mcrimes.iterator ();3 while(It.hasnext ()) {4Crime Crime =It.next ();5 if(Crime.getid (). Equals (ID)) {6 It.remove ();7 }8 }9}
A new Removecrime () method is added here to find our selected entries based on the UUID parameter passed in, and then remove it from the Mcrimes collection.
Then set the menu item's Click event in Crimefragment:
1 @Override2 Public Booleanonoptionsitemselected (MenuItem item) {3 4 Switch(Item.getitemid ()) {5 CaseR.id.delete:6 return true;7 default:return Super. onoptionsitemselected (item);8 }9}
However, there is no specific removal logic now, because the delete operation requires a UUID parameter to be passed in.
When we click on an entry in Criminalintent, the onclick () method in Crimeholder is executed.
The contents of this method contain the UUID parameters we need. What we just need is the UUID of the click entry;
Modify the code for the onclick () method in Crimelistfragment:
1 Private classCrimeholderextendsRecyclerview.viewholderImplementsview.onclicklistener{2 3 ......4 5 @Override6 Public voidOnClick (View v) {7Intent Intent =crimepageractivity.newintent (Getactivity (), Mcrime.getid ());8 Bundle args = new bundle (); 9 args.putserializable ("crime_id", Mcrime.getid ()); Crimelistfra Gment listfragment = new Crimelistfragment (); listfragment.setarguments (args); - startactivity (intent); - } the ...... - - } -
The data transfer between fragment is realized through argument.
The required UUID parameters are stored in the bundle.
In this way, the bundle object can be obtained through getarguments () in another fragment, and then the Getserializable () method of the bundle object can be obtained to obtain the incoming UUID;
Then, modify the Onoptionsitemseleted () method in Crimefragment.
1 @Override2 Public Booleanonoptionsitemselected (MenuItem item) {3 4 Switch(Item.getitemid ()) {5 CaseR.id.delete:6 7 UUID id = (UUID) getarguments (). Getserializable ("crime_id"); 9 if (id!=null) Cri Melab.get (Getactivity ()). Removecrime (ID); getactivity (). Finish (); A return true; - default:return Super. onoptionsitemselected (item); - } the}
Here first gets to the UUID and then removes the selected entry by Crimelab's Removecrime () method.
Finally, the Getactivity (). Finish () method is rolled back to the previous activity interface.
Android Authority programming Challenge Exercise 13.6