Can database entries added from items be deleted with button?

Viewed 200

I'm trying to teach myself android development using "Android Programming: The Big Nerd Ranch Guide" and one of the exercises (from chapter 14 if you are familiar with the book) involves creating a toolbar with an item that adds new entries to the database when the item is clicked. A challenge question is deleting the entries, but I want to delete the entry from WITHIN the entry. Sorry if I am not doing a good job explaining this.

Basically when opening the app for the first time, there is a mostly empty display with a toolbar with two items in the top right. Here is a screenshot if it helps. The two items add or count the number of entries, which are then displayed on the list. In the image above, there is one entry. When you click the + symbol, a new entry is added, and it takes you to the layout for the entry that lets you add all the details. I want to add a delete button that allows you to erase the entry while looking at the details.

Here is the code I tried but didn't work: In CrimeLab.java

public void addCrime(Crime c){
    ContentValues values = getContentValues(c);

    mDatabase.insert(CrimeTable.NAME, null, values);
}

public void deleteCrime(Crime crimeId){

    String uuidString = crimeId.toString();
    mDatabase.delete(CrimeTable.NAME, null, new String[] 
{uuidString});
}

I basically tried to undo what I did when creating a database entry. In CrimeListFragment.java:

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
        case R.id.menu_item_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.menu_item_show_subtitle:
            mSubtitleVisible = !mSubtitleVisible;
            getActivity().invalidateOptionsMenu();
            updateSubtitle();
            return true;
        case R.id.delete_button:
            Crime mCrime = new Crime();
            CrimeLab.get(getActivity()).deleteCrime(mCrime);
        default:
            return super.onOptionsItemSelected(item);
    }
}

I think I need to create an onClick action, but where would this go if I'm trying to delete entries from a database created by items from within the entry?

Something else I would like to add is deleting entries from the original page by sliding to reveal a delete button, like in messenger apps.

Sorry if I didn't properly ask my question. This is a whole new world for me and going along with the book is helpful, but I found its easy to think you understand something and then be completely lost on your own. Also, I checked though stackoverflow and other various forums and did not find an exact answer to this question.

Per request, here is the code for Crime.java:

public class Crime {
    private String mTitle;
    private Date mDate;
    private boolean mSolved;
    private String mSuspect;

    public Date getDate() {
        return mDate;
    }

    private UUID mId;

    public void setDate(Date date) {
        mDate = date;
    }

    public void setSolved(boolean solved) {
        mSolved = solved;
    }

    public String getSuspect(){
        return mSuspect;
    }

    public  void setSuspect (String suspect){
        mSuspect = suspect;
    }

    public String getPhotoFilename(){
        return "IMG_" + getId().toString() + ".jpg";
    }

    public boolean isSolved() {

        return mSolved;
    }



    public Crime(){
        //generate unique identifier
        this(UUID.randomUUID());
    }

    public Crime(UUID id){
        mId = id;
        mDate = new Date();
    }

    public UUID getId() {
        return mId;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }
}

This is the .java class that controls my database entries:

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 db){
        db.execSQL("create table " + CrimeTable.NAME + "(" +
                " _id integer primary key autoincrement, " +
                CrimeTable.Cols.UUID + ", " +
                CrimeTable.Cols.TITLE + ", " +
                CrimeTable.Cols.DATE + ", " +
                CrimeTable.Cols.SOLVED + ", " +
                CrimeTable.Cols.SUSPECT +
                ")"
        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

    }
}

This is where I am messing up the .onClickListener

mDeleteButton = (Button)v.findViewById(R.id.delete_button);
        mDeleteButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Crime mCrime = new crime();
                mCrime.getId()
                CrimeLab.get(getActivity()).deleteCrime(mCrime);
            }
                                         }
        );
2 Answers
Related