Challenge: Deleting Crime, An Empty View

CrimeFragment.java

	private Crime mCrime;
...
...

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// To Show Menu Bar

    setHasOptionsMenu(true);

    UUID uID = (UUID) getArguments().getSerializable(ARGS_CRIME_ID);
    mCrime = CrimeLab.get(getActivity()).getCrime(uID);
}

// Inflate menu
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.crime_delete_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.delete_crime:
	
	// Delete crimeID from CrimeLab 

            CrimeLab crimeLab = CrimeLab.get(getActivity());
            crimeLab.deleteItem(mCrime.getId());

	// Back to CrimeListFragment by Intent.

            Intent intent = new Intent(getActivity(), CrimeListActivity.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

delete_crime_menu

<item
    android:id="@+id/delete_crime"
    android:title="@string/delete_crime_here"
    android:icon="@drawable/ic_delete_crime"
    app:showAsAction="ifRoom"/>

CrimeLab
public void deleteItem(UUID crimeID) {
Crime crime = getCrime(crimeID);
mCrimes.remove(crime);
}

-----------------------------------

EmptyView::

fragment_crime_list

<android.support.v7.widget.RecyclerView
android:id=“@+id/crime_recycle_view”
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”/>

<TextView
    android:id="@+id/no_crime_there"
    android:gravity="center"
    android:layout_gravity="center"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/no_crimes_add"/>

CrimeListFragment
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);

    mCrimeRecyclerView = view.findViewById(R.id.crime_recycle_view);
    mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mTextView = view.findViewById(R.id.no_crime_there);

    if(savedInstanceState != null) {
        savedTrigger = savedInstanceState.getInt(SAVED_NUMBER_FOR_TRIGGER);
    }

    if(CrimeLab.get(getActivity()).getCrimes().size() <= 0) {
        mTextView.setVisibility(View.VISIBLE);
    } else {
        updateUI();
    }

// /* updateUI(); */
return view;
}

1 Like

Can u tell me where to define R.menu.crime_delete_menu, menu, other than Crime Fragment, you must have defined it somewhere else too.

you have to create a menu resource in menu folder :: res > new > android file resource > choose Menu as Resource type > define the name (crime_delete_menu) then start to add the items (this way work if you don’t have a Menu resource file otherwise create the file from menu folder

Thanks Man, I appreciate your support.

1 Like

Good job brother. This is impressive!
I have to add this small snippet to replace the following code…

Screenshot%20from%202018-05-13%2022-02-12

Thanks for the nice solution!

There is an alternative way to show the delete action item without adding a new menu but adding just the action item to the existing menu
.
add this to fragment_crime_list.xml and make changes in strings.xml
…
<item
android:id="@+id/remove_crime"
android:icon="@drawable/ic_menu_remove"
android:title="@string/remove_crime"
app:showAsAction=“ifRoom|withText” />

Add this instead to CrimeFragment:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.fragment_crime_list, menu);
    menu.findItem(R.id.remove_crime); // will show item in menu
    menu.removeItem(R.id.new_crime); // will remove item from menu
}

Add this to the onCreateOptionsMenu function in CrimeListFragment

menu.removeItem(R.id.remove_crime);

Hey, did you try using finish?