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());
mNotes = mDb.getNotes();
updateCurrentNote(mConfig.getCurrentNoteIndex());
updateSelectedNote(mConfig.getCurrentNoteIndex());
}
@Override
@ -102,7 +102,7 @@ public class MainActivity extends SimpleActivity {
}
}
private void updateCurrentNote(int index) {
private void updateSelectedNote(int index) {
mConfig.setCurrentNoteIndex(index);
mCurrentNote = mNotes.get(index);
mNotesView.setText(mCurrentNote.getValue());
@ -135,13 +135,31 @@ public class MainActivity extends SimpleActivity {
} else if (mDb.doesTitleExist(title)) {
Utils.showToast(getApplicationContext(), R.string.title_taken);
} 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();
}
}
});
}
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() {
final Resources res = getResources();
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
@ -161,6 +179,9 @@ public class MainActivity extends SimpleActivity {
if (mNotes.size() <= 1)
return;
mDb.deleteNote(mCurrentNote.getId());
mNotes = mDb.getNotes();
updateSelectedNote(0);
}
private void displayOpenNoteDialog() {
@ -176,7 +197,7 @@ public class MainActivity extends SimpleActivity {
builder.setItems(notes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
updateCurrentNote(which);
updateSelectedNote(which);
}
});
builder.show();

View File

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

View File

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