An example of the SQLite Database (Part 2)

How to make Note App using SQLite Database in Android Studio Part 2

Display Notes with edit and deletes option
The alert dialog to update

activity_all_notes.xml

row_note.xml

MySqliteHelper.java

public List<note_model> getAllNotes(){
SQLiteDatabase database= this.getReadableDatabase();
String[] cols={TableSchema.note.ID,TableSchema.note.TITLE,TableSchema.note.DESCRIPTION,TableSchema.note.DATE};
Cursor cursor=database.query(TableSchema.note.TABLE_NAME,cols,null,null,null,null,TableSchema.note.ID+" DESC");
ArrayList<note_model> list=new ArrayList<>();
while (cursor.moveToNext()){
note_model model=new note_model();
model.setId(cursor.getInt(cursor.getColumnIndex(TableSchema.note.ID)));
model.setTitle(cursor.getString(cursor.getColumnIndex(TableSchema.note.TITLE)));
model.setDescription(cursor.getString(cursor.getColumnIndex(TableSchema.note.DESCRIPTION)));
model.setDate(cursor.getString(cursor.getColumnIndex(TableSchema.note.DATE)));
list.add(model);
}
cursor.close();
database.close();
return list;
}

getAllNotes

ArrayList<note_model> list=new ArrayList<>();
list.add(model);

Deleting the Notes

public boolean delNoteById(String id){
SQLiteDatabase database=this.getWritableDatabase();
int d_id=database.delete(TableSchema.note.TABLE_NAME,TableSchema.note.ID+"=?",new String[]{id});
if (d_id==-1){
database.close();
return false;
}
database.close();
return true;
}

Updating the notes

public boolean updateNoteById(note_model model){
SQLiteDatabase database=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(TableSchema.note.TITLE,model.getTitle());
values.put(TableSchema.note.DESCRIPTION,model.getDescription());
values.put(TableSchema.note.DATE,model.getDate());
int u_id=database.update(TableSchema.note.TABLE_NAME,values,TableSchema.note.ID+"=?",new String[]{String.valueOf(model.getId())});
if (u_id==-1){
database.close();
return false;
}
database.close();
return true;
}

NoteAdapter.java

AllNotesActivity.java

I’m a good husband, father and Senior Developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store