Challenge: Deleting Crime, An Empty View


#1

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;
}


#2

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.


#3

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


#4

Thanks Man, I appreciate your support.


#5

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