implement deleting notes

This commit is contained in:
tibbi 2016-10-02 18:25:09 +02:00
parent ee8e8dd17c
commit adac7cbf28
3 changed files with 41 additions and 6 deletions

View File

@ -43,7 +43,7 @@ public class MainActivity extends SimpleActivity {
mDb = DBHelper.newInstance(getApplicationContext()); mDb = DBHelper.newInstance(getApplicationContext());
mNotes = mDb.getNotes(); mNotes = mDb.getNotes();
updateCurrentNote(mConfig.getCurrentNoteIndex()); updateSelectedNote(mConfig.getCurrentNoteIndex());
} }
@Override @Override
@ -102,7 +102,7 @@ public class MainActivity extends SimpleActivity {
} }
} }
private void updateCurrentNote(int index) { private void updateSelectedNote(int index) {
mConfig.setCurrentNoteIndex(index); mConfig.setCurrentNoteIndex(index);
mCurrentNote = mNotes.get(index); mCurrentNote = mNotes.get(index);
mNotesView.setText(mCurrentNote.getValue()); mNotesView.setText(mCurrentNote.getValue());
@ -135,13 +135,31 @@ public class MainActivity extends SimpleActivity {
} else if (mDb.doesTitleExist(title)) { } else if (mDb.doesTitleExist(title)) {
Utils.showToast(getApplicationContext(), R.string.title_taken); Utils.showToast(getApplicationContext(), R.string.title_taken);
} else { } else {
mDb.insertNote(new Note(0, title, "")); final Note newNote = new Note(0, title, "");
final int id = mDb.insertNote(newNote);
newNote.setId(id);
mNotes = mDb.getNotes();
final int newNoteIndex = getNewNoteIndex(newNote);
updateSelectedNote(newNoteIndex);
alertDialog.dismiss(); alertDialog.dismiss();
} }
} }
}); });
} }
private int getNewNoteIndex(Note note) {
final int cnt = mNotes.size();
int index = 0;
for (int i = 0; i < cnt; i++) {
if (mNotes.get(i).equals(note)) {
index = i;
break;
}
}
return index;
}
private void displayDeleteNotePrompt() { private void displayDeleteNotePrompt() {
final Resources res = getResources(); final Resources res = getResources();
final AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog.Builder builder = new AlertDialog.Builder(this);
@ -161,6 +179,9 @@ public class MainActivity extends SimpleActivity {
if (mNotes.size() <= 1) if (mNotes.size() <= 1)
return; return;
mDb.deleteNote(mCurrentNote.getId());
mNotes = mDb.getNotes();
updateSelectedNote(0);
} }
private void displayOpenNoteDialog() { private void displayOpenNoteDialog() {
@ -176,7 +197,7 @@ public class MainActivity extends SimpleActivity {
builder.setItems(notes, new DialogInterface.OnClickListener() { builder.setItems(notes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
updateCurrentNote(which); updateSelectedNote(which);
} }
}); });
builder.show(); builder.show();

View File

@ -65,9 +65,9 @@ public class DBHelper extends SQLiteOpenHelper {
db.insert(TABLE_NAME, null, values); db.insert(TABLE_NAME, null, values);
} }
public void insertNote(Note note) { public int insertNote(Note note) {
final ContentValues values = fillContentValues(note); final ContentValues values = fillContentValues(note);
mDb.insert(TABLE_NAME, null, values); return (int) mDb.insert(TABLE_NAME, null, values);
} }
private ContentValues fillContentValues(Note note) { private ContentValues fillContentValues(Note note) {
@ -77,6 +77,10 @@ public class DBHelper extends SQLiteOpenHelper {
return values; return values;
} }
public void deleteNote(int id) {
mDb.delete(TABLE_NAME, COL_ID + " = " + id, null);
}
public boolean doesTitleExist(String title) { public boolean doesTitleExist(String title) {
final String cols[] = {COL_ID}; final String cols[] = {COL_ID};
final String selection = COL_TITLE + " = ?"; final String selection = COL_TITLE + " = ?";

View File

@ -23,6 +23,10 @@ public class Note {
return mValue; return mValue;
} }
public void setId(int id) {
mId = id;
}
public void setValue(String value) { public void setValue(String value) {
mValue = value; mValue = value;
} }
@ -31,6 +35,12 @@ public class Note {
mTitle = title; mTitle = title;
} }
@Override
public boolean equals(Object o) {
return o != null && this.toString().equals(o.toString());
}
@Override @Override
public String toString() { public String toString() {
return "Note {" + return "Note {" +