Exercise session 6
05.12.2017
Overview
We’ll create a notes app.
With EditText, Button and RecyclerView
We’ll save the notes to the database.
On user click we’ll add note to the database
User click on note we will remove it from database
What is Room?
Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.
There are 3 major components in Room
Let’s Begin
You can always use This project as a reference
And Room training Guide can be helpful too.
Step1- Create a new project
Step2- Add Room dependencies
Add Room to dependencies in build.gradle(app) file:
implementation "android.arch.persistence.room:runtime:1.0.0"�annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
GitHub commit: https://goo.gl/TfmEpH
Step3- Create Note Entity class
@Entity�public class Note {� @PrimaryKey(autoGenerate = true)� private int id;
GitHub commit: https://goo.gl/3jE2dd
Step4- Add AppDatabase class
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),� AppDatabase.class, DATABASE_NAME)� .allowMainThreadQueries()� .build();
GitHub commit: https://goo.gl/Hx4BBa
Step5- Add NoteDao interface
*** Do not forget to add abstract method in AppDatabase that returns NoteDao
public abstract NoteDao noteDao();
GitHub commit: https://goo.gl/gD3TPc
NoteDao - query data
@Query("SELECT * FROM Note")�List<Note> getAll();��@Query("SELECT * FROM Note WHERE id = :id")�Note getNoteById(int id);
NoteDao - insert row/s
@Insert(onConflict = OnConflictStrategy.REPLACE)�void insertAll(Note... notes);��@Insert(onConflict = OnConflictStrategy.REPLACE)�void insert(Note note);
NoteDao - delete row
@Delete�void delete(Note note);
Step6 - Add RecyclerView
mNotesAdapter = new NotesAdapter(this, getNoteList());�recyclerView.setAdapter(mNotesAdapter);
private List<Note> getNoteList() {� List<Note> list = new ArrayList<>();� list.add(createNote("Title 1", "Body 1"));� list.add(createNote("Title 2", "Body 2"));� list.add(createNote("Title 3", "Body 3"));� return list;�}
private Note createNote(String title, String body) {� Note note = new Note();� note.setTitle(title);� note.setBody(body);� return note;�}
GitHub commit: https://goo.gl/wbJstm
Step7- Add fake data to DB and use it in adapter
private List<Note> getNoteList() {� AppDatabase.getInstance(this).noteDao().insertAll(� createNote("Title 1", "Body 1"),� createNote("Title 2", "Body 2"),� createNote("Title 3", "Body 3")� );� return AppDatabase.getInstance(this).noteDao().getAll();�}
GitHub commit: https://goo.gl/ot6o3h
Step8 - Delete notes from DB on item click
public void onClick(View view) {� int position = (int) view.getTag();� Note note = mNoteList.get(position);� mListener.onDeleteNote(note);� mNoteList.remove(note);� notifyItemRemoved(position);� notifyItemRangeChanged(position, mNoteList.size());�}
GitHub commit: https://goo.gl/SYuyfb
Step9 - Remove fake data
GitHub commit: https://goo.gl/kTpB9b
Step10- Add adding note UI elements
GitHub commit: https://goo.gl/RVhCxD
Step11- Add note to DB
When user click on “Add note” button get inputs from EditTexts and save them to the database
AppDatabase.getInstance(this).noteDao().insert(note);
GitHub commit: https://goo.gl/k8m3QQ
Step12- Update the list when new note is added
MainActivity
NotesAdapter
List<Note> updatedList = getNoteList();�mNotesAdapter.updateList(updatedList);� � �public void updateList(List<Note> updatedList) {� mNoteList = updatedList;� notifyDataSetChanged();�}
GitHub commit: https://goo.gl/946ZCe
DONE?
That’s amazing! Good for you!!
If you have even a small question- don’t forget to ask the mentors:
At the class or on Slack.
Kudos!!
You’re all done with exercise 6!
See you next time!