How to Delete Data in Realm Database in Android?
In the previous series of articles on the realm database, we have seen adding, reading, and updating data using the realm database in android. In that articles, we were adding course details, reading them, and updating them. In this article, we will take a look at deleting these course details from our realm database in the Android app.
What we are going to build in this article?
We will be building a simple application in which we will be working on the existing applications in which we will be simply adding a new button to delete our course from the realm database. Below is the video in which we will get to see what we are going to build in this article.
Step by Step Implementation
Step 1: Add google repository in the build.gradle file of the application project.
All Jetpack components are available in the Google Maven repository, include them in the build.gradle file
Step 2: Working with the activity_update_course.xml file
Navigate to the app > res > layout > activity_update_course.xml file and add a Button inside this layout for deleting a course. Below is the code for that file.
Below is the updated code for the activity_update_course.xml file after adding the above code snippet.
Step 3: Working with the UpdateCourseActivity.java file
Initializing our button to delete our course. Navigate to the app > java > your app’s package name > UpdateCourseActivity.java file and add the below code to it.
Below is the updated code for the UpdateCourseActivity.java file after adding the above code snippet.
Now run your app and see the output of the code:
Below is the complete project file structure after performing the CRUD operation:
Check out the project at the below link: https://github.com/ChaitanyaMunje/Realm-Db
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.