I'm having issues when trying to delete a Crime from my criminal intent app using Android java programming. How would I write a deleteCrime() method that deletes the saved crime from my database after the delete button is clicked in my app?

Code where the case for when the button is clicked

##################################################### public class CrimeListFragment extends Fragment { private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private Crime mCrime; private TextView mTitleTextView; private TextView mDateTextView; private ImageView mCrimeSolved; public CrimeHolder(LayoutInflater inflater, ViewGroup parent) { super(inflater.inflate(R.layout.list_item_crime, parent, false)); itemView.setOnClickListener(this); mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title); mDateTextView = (TextView) itemView.findViewById(R.id.crime_date); mCrimeSolved = (ImageView) itemView.findViewById(R.id.crime_solved); } public void bind(Crime crime) { mCrime = crime; mTitleTextView.setText(mCrime.getTitle()); mDateTextView.setText(mCrime.getDate().toString()); mCrimeSolved.setVisibility(crime.isSolved() ? View.VISIBLE : View.INVISIBLE); } @Override public void onClick(View view) { Intent intent = CrimePagerActivity.newIntent(getActivity(), mCrime.getId()); startActivity(intent); } } private class CrimeAdapter extends RecyclerView.Adapter { private List mCrimes; public CrimeAdapter(List crimes) { mCrimes = crimes; } @NonNull @Override public CrimeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); Log.d(“adapter”, “Creating”); return new CrimeHolder(layoutInflater, parent); } @Override public void onBindViewHolder(@NonNull CrimeHolder crimeHolder, int i) { Crime crime = mCrimes.get(i); Log.d(“adapter”, “Binding”); crimeHolder.bind(crime); } public void setCrimes(List crimes) { mCrimes = crimes; } @Override public int getItemCount() { return mCrimes.size(); } } private RecyclerView mCrimeRecyclerView; private CrimeAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime_list, container, false); mCrimeRecyclerView = (RecyclerView) v.findViewById(R.id.crime_recycler_view); mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); updateUI(); return v; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.fragment_crime_list, menu); MenuItem deleteItem = menu.findItem(R.id.delete_button); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.new_crime: Crime crime = new Crime(); CrimeLab.get(getActivity()).addCrime(crime); Intent intent = CrimePagerActivity.newIntent(getActivity(), crime.getId()); startActivity(intent); return true; case R.id.delete_button: // CrimeLab.get(getActivity()).deleteCrime(); getActivity().finish(); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onResume() { super.onResume(); updateUI(); } private void updateUI() { CrimeLab crimeLab = CrimeLab.get(getActivity()); List crimes = crimeLab.getCrimes(); if(mAdapter == null) { mAdapter = new CrimeAdapter(crimes); mCrimeRecyclerView.setAdapter(mAdapter); } else { mAdapter.setCrimes(crimes); mAdapter.notifyDataSetChanged(); } } }

########################################################################

DB

######################################################################## public class CrimeDbSchema { public static final class CrimeTable { public static final String NAME = “crimes”; public static final class Cols { public static final String UUID = “uuid”; public static final String TITLE = “title”; public static final String DATE = “date”; public static final String SOLVED = “solved”; } } }

#####################################

DBHelper

#################################### public class CrimeBaseHelper extends SQLiteOpenHelper { private static final int VERSION = 1; private static final String DATABASE_NAME = “crimeBase.db”; public CrimeBaseHelper(Context context) { super(context, DATABASE_NAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL(“create table ” + CrimeDbSchema.CrimeTable.NAME + “(” + ” _id integer primary key autoincrement, ” + CrimeDbSchema.CrimeTable.Cols.UUID + “, ” + CrimeDbSchema.CrimeTable.Cols.TITLE + “, ” + CrimeDbSchema.CrimeTable.Cols.DATE + “, ” + CrimeDbSchema.CrimeTable.Cols.SOLVED + “)”); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { } }

#################################################

DBCursorWrapper

################################################# public class CrimeCursorWrapper extends CursorWrapper { public CrimeCursorWrapper(Cursor cursor) { super(cursor); } public Crime getCrime() { String uuidString = getString(getColumnIndex(CrimeDbSchema.CrimeTable.Cols.UUID)); String title = getString(getColumnIndex(CrimeDbSchema.CrimeTable.Cols.TITLE)); long date = getLong(getColumnIndex(CrimeDbSchema.CrimeTable.Cols.DATE)); int isSolved = getInt(getColumnIndex(CrimeDbSchema.CrimeTable.Cols.SOLVED)); Crime crime = new Crime(UUID.fromString(uuidString)); crime.setTitle(title); crime.setDate(new Date(date)); crime.setSolved(isSolved != 0); return crime; } }

####################################################################################

CrimeLab Class where deleteCrime() method is held

################################################################################### public class CrimeLab { private static CrimeLab sCrimeLab; private Context mContext; private SQLiteDatabase mDatabase; public static CrimeLab get(Context context) { if(sCrimeLab == null) { sCrimeLab = new CrimeLab(context); } return sCrimeLab; } private CrimeLab(Context context) { mContext = context.getApplicationContext(); mDatabase = new CrimeBaseHelper(mContext).getWritableDatabase(); } public List getCrimes() { List crimes = new ArrayList<>(); CrimeCursorWrapper cursor = queryCrimes(null, null); try { cursor.moveToFirst(); while(!cursor.isAfterLast()) { crimes.add(cursor.getCrime()); cursor.moveToNext(); } } finally { cursor.close(); } return crimes; } public void addCrime(Crime c) { ContentValues values = getContentValues(c); mDatabase.insert(CrimeDbSchema.CrimeTable.NAME, null, values); } public void deleteCrime(Crime c) { String uuidString = c.getId().toString(); ContentValues values = getContentValues(c); mDatabase.delete(CrimeDbSchema.CrimeTable.NAME,CrimeDbSchema.CrimeTable.Cols.UUID,new String[]{uuidString}); } public Crime getCrime(UUID id) { CrimeCursorWrapper cursor = queryCrimes(CrimeDbSchema.CrimeTable.Cols.UUID + ” =?”, new String[]{ id.toString()}); try{ if(cursor.getCount() == 0) { return null; } cursor.moveToFirst(); return cursor.getCrime(); }finally { cursor.close(); } } public void updateCrime(Crime crime) { String uuidString = crime.getId().toString(); ContentValues values = getContentValues(crime); mDatabase.update(CrimeDbSchema.CrimeTable.NAME, values, CrimeDbSchema.CrimeTable.Cols.UUID + ” =?”, new String[] { uuidString }); } private static ContentValues getContentValues(Crime crime) { ContentValues values = new ContentValues(); values.put(CrimeDbSchema.CrimeTable.Cols.UUID, crime.getId().toString()); values.put(CrimeDbSchema.CrimeTable.Cols.TITLE, crime.getTitle()); values.put(CrimeDbSchema.CrimeTable.Cols.DATE, crime.getDate().getTime()); values.put(CrimeDbSchema.CrimeTable.Cols.SOLVED, crime.isSolved() ? 1 : 0); return values; } private CrimeCursorWrapper queryCrimes(String whereClause, String[] whereArgs) { Cursor cursor = mDatabase.query(CrimeDbSchema.CrimeTable.NAME, null, whereClause, whereArgs, null, null, null); return new CrimeCursorWrapper(cursor); } }

##################################################################################################

"Get 15% discount on your first 3 orders with us"
Use the following coupon
"FIRST15"

Order Now