Hello and welcome to this Android Room Tutorial from CodingWithChris. In this series, i want you to show, how simple you can create an app, which uses the Android Room library to store data.
I created for each part of this series a branch, so you do not have to start from the very beginning. In my Videos, i will explain, how you can use this repository. In the follwing, you get the Tasks, which are already implemented in the different branches:
This will be the start of the whole video series. In this branch, i just create a simple project in Android Studio.
The following Tasks are implemented in this branch:
- Add RecyclerView to fragment_main.xml
- Create item_voc.xml as childlayout for the RecyclerView
- Create the VocListAdapter (RecyclerView.Adapter) for all available Vocabularies
- Implement the RecyclerView in MainFragment.kt
- Create a new Drawable for the FloatingActionButton
-
Add the Gradle Dependencies
-
Create the Voc Entity Class
-
Create the Data Access Object (DAO)
-
Create the VocDataBase Class
-
Create a Repository
-
Create the MainViewModel.kt
-
Create the MainViewModelFactory
-
Implement the Methods to interact with Room @ MainViewModel.kt
-
Create a simple Input Dialog with DialogFragment
1.1) Modify the style.xml
1.2) Create layout file - dialog_input.xml like the following example
1.3) Create DialogFragment() - DialogInput.kt
1.4) Save User Input to MainViewModel with fun insert(voc:Voc)
1.5) Connect the FloatingActionButton @ MainActivity.kt with the DialogInput.kt
-
Register Observer on the VocList LiveData and Update VocListAdapter
2.1) Create MainViewModel Instance
2.2) Update VocListAdapter with method fun updateContent(..) and change ArrayList Type from String to Voc
2.3) Override OnBindViewHolder(...) @ VocListAdapter.kt
2.4) Implement Observer @ MainFragment.kt
-
Set clickable Background for the Item Layout item_voc.xml
-
Create an OnClickListener for each Item of the RecyclerView - show an entry
2.1) Create an Interface OnItemClickListener VocListAdapter.kt
2.2) Implement the Interface in MainFragment.kt
2.3) Update the InputDialog.kt, that you can transfer an Voc Object, which will be default to null. Depending on the status of the Voc, we set the Texts of the EditTexts and call the update(voc:Voc) method, instead of the insert(...) method of the MainViewModel.
2.4) Call the InputDialog.kt with a click on an item
-
Create an OnLongClickListener for each item of the RecyclerView - Delete an entry
3.1) Create an Interface OnItemLongClickListener
3.2) Create a simple AlarmDialog to confirm the delete action in MainFragment.kt and call the fun delete(voc:Voc) method to remove the entry, if user confirm delete action
3.3) Implement the Interface in MainFragment.kt and call the AlarmDialog to confirm the delete action
