![]() |
VOOZH | about |
Data from the app can be saved on users' devices in different ways. We can store data in the user's device in SQLite tables, shared preferences, and many more ways. In this article, we will take a look at saving data, reading, updating, and deleting data in Room Database on Android. We will perform CRUD operations using Room Database on Android. In this article, we will take a look at performing CRUD operations in Room Database in Android.
We will be building a simple application in which we will be adding the different types of courses that are available on Geeks for Geeks. We will be storing all this data in Room Database and performing CRUD operations on these courses. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Room is a persistence library that provides an abstraction layer over the SQLite database to allow a more robust database. With the help of room, we can easily create the database and perform CRUD operations very easily.
The three main components of the room are Entity, Database, and DAO.
Now, let's move towards the implementation of Room Database in Android.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Adding dependency for using Room in build.gradle files
Navigate to the app > Gradle Scripts > build.gradle file and add the below dependencies in the dependencies section.
// add below dependency for using room.
implementation 'androidx.room:room-runtime:2.2.5'
annotationProcessor 'androidx.room:room-compiler:2.2.5'
// add below dependency for using lifecycle extensions for room.
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.2.0'
After adding the above dependencies section. Now sync your project and we will move towards our XML file.
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 4: Creating a modal class for storing our data
Navigate to the app > java > your apps package name > Right-click on it > New > Java class and name the class as CourseModal and add the below code to it. Comments are added inside the code to understand the code in more detail.
Step 5: Creating a Dao interface for our database
Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name as Dao and select Interface. After creating an interface class and add the below code to it. Comments are added inside the code to understand the code in more detail.
Step 6: Creating a database class
Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name it as CourseDatabase and add the below code to it. Comments are added inside the code to understand the code in more detail.
Step 7: Create a new java class for our Repository
Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name it as CourseRepository and add the below code to it. Comments are added inside the code to understand the code in more detail.
Step 8: Creating a class for our Repository
Navigate to the app > java > your app's package name > Right-click on it > New > Java Class and name the class as ViewModal and add the below code to it. Comments are added inside the code to understand the code in more detail.
Step 9: Creating a layout file for each item of RecyclerView
Navigate to the app > res > layout > Right-click on it > New > layout resource file and name it as course_rv_item and add below code to it. Comments are added in the code to get to know in more detail.
Step 10: Creating a RecyclerView Adapter class to set data for each item of RecyclerView
Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name it as CourseRVAdapter and add the below code to it. Comments are added inside the code to understand the code in more detail.
Step 11: Creating a new Activity for Adding and Updating our Course
Navigate to the app > java > your app's package name > Right-click on it > New > Empty Activity and name it as NewCourseActivity and go to XML part and add below code to it. Below is the code for the activity_new_course.xml file.
Step 12: Working with the NewCourseActivity.java file
Navigate to the app > java > your app's package name > NewCourseActivity.java file and add the below code to it. Comments are added inside the code to understand the code in more detail.
Step 13: Working with the MainActivity.java file
Navigate to the app > java > your app's package name > MainActivity.java file and add the below code to it. Comments are added inside the code to understand the code in more detail.
Now run your app and see the output of the app.
Check out the project on the below link:https://github.com/ChaitanyaMunje/GFG-Room-Database