Potential Challenge Solution? Deleting from CrimeFragment


#1

I added a menu called “challenge_fragment_crime_menu.xml” as seen below. I also added an icon in “drawable-hdpi” called ic_delete:

[b]<?xml version="1.0" encoding="utf-8"?>

[/b]

The code I changed in CrimeFragment.java:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.challenge_delete:
CrimeLab.get(getActivity()).deleteCrime(mCrime);
getActivity().finish();
return true;

case android.R.id.home:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.challenge_fragment_crime_menu, menu);

}

Is this a good way to do this?


#2

Yep!


#3

Just a note:

Remember that switch-case functions work as follows:

  1. Evaluate Switch to proper Case.
  2. Run code from that point on (including the next cases) until ‘return’ call.

A clever way to reduce code in this challenge is to put the Delete case right before the NavUtil “up/home” case, and don’t put a ‘return’ statement after your delete code. This way, if you press ‘delete’, it will delete the crime, then automatically start running the “NavUtil Up/Home Navigation” code (with it’s OWN call to ‘return’), bringing you automatically to the ListView fragment/activity.

This only saves a couple lines of code, but a little trick to consider! :wink:


#4

I did the same thing save for one exception: I set android:showAsAction to “never”, ensuring that the button is only available within the overflow menu. I’m just thinking that from a UX perspective, you don’t want an active delete button lying around on the screen for the user to accidentally press. There should have to be some concerted effort involved.


#5

is there a benefit in not using the already existing crime_list_item_context.xml file that contains the delete item


#6

I finish this challange
without this line

getActivity().finish(); return true;

what is the function of

???

and for the delete code in crimeListFragment.java
we need to do

for notify the adapter if the dataset have changed(get deleted)
but why in CrimeFragment not?