![]() |
VOOZH | about |
We have seen How to Create and Add Data to SQLite Database in Android as well as How to Read Data from SQLite Database in Android. We have performed different SQL queries for reading and writing our data to SQLite database. In this article, we will take a look at updating data to SQLite database in Android.
We will be building a simple application in which we were already adding as well as reading the data. Now we will simply update our data in a new activity and we can get to see the updated data. 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.
Step 1: Creating a new activity for updating our course
As we want to update our course, so for this process we will be creating a new activity where we will be able to update our courses in the SQLite database. To create a new Activity we have to navigate to the app > java > your app’s package name > Right click on package name > New > Empty Activity and name your activity as UpdateCourseActivity and create new Activity. Make sure to select the empty activity.
Step 2: Working with the activity_update_course.xml file
Navigate to the app > res > Layout > activity_update_course.xml and add the below code to it.
Step 3: Updating our DBHandler class
Navigate to the app > java > your app's package name > DBHandler and add the below code to it. In this, we simply have to create a new method to update our course.
Below is the updated code for the DBHandler.java file after adding the above code snippet.
Step 4: Updating our CourseRVAdapter.java class
As we will be opening a new activity to update our course. We have to add on click listener for the items of our RecycleView. For adding onClickListener() to our recycler view items navigate to the app > java > your app's package name > CourseRVAdapter class and simply add an onClickListener() for our RecyclerView item. Add the below code to your adapter class.
Below is the updated code for the CourseRVAdapter.java file after adding the above code snippet.
Step 5: Working with the UpdateCourseActivity.java file
Navigate to the app > java > your app's package name > UpdateCourseActivity.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. Make sure to add data to the SQLite database before updating it.
Below is the complete project file structure after performing the update operation: