Links & DependenciesNoteRepository.javaNoteDao.javaNoteDatabase.javaNote.java
“Adding Components” page with dependencies & instructions:
developer.android.com/topic/libraries/architecture/adding-components
CardView & Design Support Library:
developer.android.com/topic/libraries/support-library/packages#v7-cardview
developer.android.com/topic/libraries/support-library/packages#design
package com.codinginflow.architectureexample; import android.app.Application; import android.arch.lifecycle.LiveData; import android.os.AsyncTask; import java.util.List; public class NoteRepository { private NoteDao noteDao; private LiveData<List<Note>> allNotes; public NoteRepository(Application application) { NoteDatabase database = NoteDatabase.getInstance(application); noteDao = database.noteDao(); allNotes = noteDao.getAllNotes(); } public void insert(Note note) { new InsertNoteAsyncTask(noteDao).execute(note); } public void update(Note note) { new UpdateNoteAsyncTask(noteDao).execute(note); } public void delete(Note note) { new DeleteNoteAsyncTask(noteDao).execute(note); } public void deleteAllNotes() { new DeleteAllNotesAsyncTask(noteDao).execute(); } public LiveData<List<Note>> getAllNotes() { return allNotes; } private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void> { private NoteDao noteDao; private InsertNoteAsyncTask(NoteDao noteDao) { this.noteDao = noteDao; } @Override protected Void doInBackground(Note... notes) { noteDao.insert(notes[0]); return null; } } private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void> { private NoteDao noteDao; private UpdateNoteAsyncTask(NoteDao noteDao) { this.noteDao = noteDao; } @Override protected Void doInBackground(Note... notes) { noteDao.update(notes[0]); return null; } } private static class DeleteNoteAsyncTask extends AsyncTask<Note, Void, Void> { private NoteDao noteDao; private DeleteNoteAsyncTask(NoteDao noteDao) { this.noteDao = noteDao; } @Override protected Void doInBackground(Note... notes) { noteDao.delete(notes[0]); return null; } } private static class DeleteAllNotesAsyncTask extends AsyncTask<Void, Void, Void> { private NoteDao noteDao; private DeleteAllNotesAsyncTask(NoteDao noteDao) { this.noteDao = noteDao; } @Override protected Void doInBackground(Void... voids) { noteDao.deleteAllNotes(); return null; } } }
package com.codinginflow.architectureexample; import android.arch.lifecycle.LiveData; import android.arch.persistence.room.Dao; import android.arch.persistence.room.Delete; import android.arch.persistence.room.Insert; import android.arch.persistence.room.Query; import android.arch.persistence.room.Update; import java.util.List; @Dao public interface NoteDao { @Insert void insert(Note note); @Update void update(Note note); @Delete void delete(Note note); @Query("DELETE FROM note_table") void deleteAllNotes(); @Query("SELECT * FROM note_table ORDER BY priority DESC") LiveData<List<Note>> getAllNotes(); }
package com.codinginflow.architectureexample; import android.arch.persistence.db.SupportSQLiteDatabase; import android.arch.persistence.room.Database; import android.arch.persistence.room.Room; import android.arch.persistence.room.RoomDatabase; import android.content.Context; import android.os.AsyncTask; import android.support.annotation.NonNull; @Database(entities = {Note.class}, version = 1) public abstract class NoteDatabase extends RoomDatabase { private static NoteDatabase instance; public abstract NoteDao noteDao(); public static synchronized NoteDatabase getInstance(Context context) { if (instance == null) { instance = Room.databaseBuilder(context.getApplicationContext(), NoteDatabase.class, "note_database") .fallbackToDestructiveMigration() .addCallback(roomCallback) .build(); } return instance; } private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() { @Override public void onCreate(@NonNull SupportSQLiteDatabase db) { super.onCreate(db); new PopulateDbAsyncTask(instance).execute(); } }; private static class PopulateDbAsyncTask extends AsyncTask<Void, Void, Void> { private NoteDao noteDao; private PopulateDbAsyncTask(NoteDatabase db) { noteDao = db.noteDao(); } @Override protected Void doInBackground(Void... voids) { noteDao.insert(new Note("Title 1", "Description 1", 1)); noteDao.insert(new Note("Title 2", "Description 2", 2)); noteDao.insert(new Note("Title 3", "Description 3", 3)); return null; } } }
package com.codinginflow.architectureexample; import android.arch.persistence.room.Entity; import android.arch.persistence.room.PrimaryKey; @Entity(tableName = "note_table") public class Note { @PrimaryKey(autoGenerate = true) private int id; private String title; private String description; private int priority; public Note(String title, String description, int priority) { this.title = title; this.description = description; this.priority = priority; } public void setId(int id) { this.id = id; } public int getId() { return id; } public String getTitle() { return title; } public String getDescription() { return description; } public int getPriority() { return priority; } }