"Android Programming Definitive Guide (Third Edition)" Chapter 14 Challenge Exercise Demo

"Android Programming Definitive Guide (Third Edition)" Chapter 14 Challenge Exercise Demo

The challenge exercises in this chapter are almost the same as those in Chapter 13, just to operate the database, talk less nonsense, and paste the code directly:

/**
     * The method of delete is slightly different, use uuid to determine which crime is deleted
     */
    public void deleteCrime(Crime c) {
        mDatabase.delete(CrimeTable.NAME, "uuid = ?", new String[] {String.valueOf(c.getId())});
    }

Add a delete method in CrimeLab

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_crime_delete, menu);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.delete_crime:
                /**
                 * Call the delete method of CrimeLab
                 */
                CrimeLab.get(getActivity()).deleteCrime(mCrime);
                getActivity().finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

Add a button to the toolbar in CrimeFragment to implement the delete function.

If there are any shortcomings, please advise, source code link: Demo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325586833&siteId=291194637