check if the title is free at creating a new note

This commit is contained in:
tibbi 2016-10-01 19:26:43 +02:00
parent 5f4001e71a
commit eebab76151
2 changed files with 16 additions and 0 deletions

View File

@ -101,6 +101,8 @@ public class MainActivity extends SimpleActivity {
final String title = titleET.getText().toString().trim();
if (title.isEmpty()) {
Utils.showToast(getApplicationContext(), R.string.no_title);
} else if (mDb.doesTitleExist(title)) {
Utils.showToast(getApplicationContext(), R.string.title_taken);
} else {
alertDialog.dismiss();
}

View File

@ -74,6 +74,20 @@ public class DBHelper extends SQLiteOpenHelper {
return values;
}
public boolean doesTitleExist(String title) {
final String cols[] = {COL_ID};
final String selection = COL_TITLE + " = ?";
final String selectionArgs[] = {title};
Cursor cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null);
if (cursor == null)
return false;
final int cnt = cursor.getCount();
cursor.close();
return cnt == 1;
}
public void updateNote(Note note) {
final ContentValues values = fillContentValues(note);
final String selection = COL_ID + " = ?";