show "No Title" if a column in SQLite is empty

Sam Atkinson :

I made a note app and I give users an option to leave the title of their note empty. But in RecyclerView and Toolbar title is shown and in this case it's empty. I tried to give value to that column while creating the table but it doesn't work.

Here's the code to create table:

@Override
public void onCreate(SQLiteDatabase db) {

    String query = "CREATE TABLE " + DATABASE_TABLE + "(" +
            ID + " INTEGER PRIMARY KEY," +
            TITLE + " TEXT DEFAULT 'No Title'," +
            CONTENT + " TEXT," +
            DATE + " TEXT," +
            TIME + " TEXT" + ")";
    db.execSQL(query);
}

UPDATE:

Save method in adding note:

 private void saveNote() {
    if (TextUtils.isEmpty(title.getText()) && TextUtils.isEmpty(content.getText())) {
        Toast.makeText(this, "Please write Something in your note", Toast.LENGTH_SHORT).show();
    } else {
        NotePad notePad = new NotePad(title.getText().toString(), content.getText().toString(), currentDate, currentTime);
        Database database = new Database(this);
        database.addNote(notePad);
        Toast.makeText(this, "Note Saved!", Toast.LENGTH_LONG).show();
        moveToList();
    }

}

add note method in database:

void addNote(NotePad notePad) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(TITLE, notePad.getTitle());
    cv.put(CONTENT, notePad.getContent());
    cv.put(DATE, notePad.getDate());
    cv.put(TIME, notePad.getTime());

    database.insert(DATABASE_TABLE, null, cv);
}
forpas :

In the method addNote() you should not set a value in the ContentValues object for TITLE if it is empty, so SQLite will set the column's value to the default value:

void addNote(NotePad notePad) {
    SQLiteDatabase database = this.getWritableDatabase();
    cv = new ContentValues();
    if (!TextUtils.isEmpty(notePad.getTitle())) cv.put(TITLE, notePad.getTitle());
    cv.put(CONTENT, notePad.getContent());
    cv.put(DATE, notePad.getDate());
    cv.put(TIME, notePad.getTime());

    database.insert(DATABASE_TABLE, null, cv);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=175286&siteId=1