How to Update Data in Firebase Firestore in Android?
In the previous article, we have seen on How to Add Data to Firebase Firestore in Android, How to Read the data from Firebase Firestore in Android. Now we will see How to Update this added data inside our Firebase Firestore. Now we will move towards the implementation of this updating data in Android Firebase.
What we are going to build in this article?
We will be creating a similar screen as we were creating for adding the data and inside this screen, we will be updating our data inside our Firebase Firestore and that data will also be updated inside our app.
Step by Step Implementation
Step 1: Creating a new Activity for updating the data
For creating a new Activity navigate to the app > res > layout > Right-Click on it and click on New > then click on Empty Activity to create a new Activity and we will name it as UpdateCourse. After creating a new Activity navigate to the app > res > layout > activity_update_course.xml and add the below code to it.
Step 2: Updating our Modal Class where we were storing our data
In previous articles, we have seen creating our Modal class. In this article, we will update our Modal class so that we can pass our object class from adapter to our activity for navigation. Along with the implementation of serializable, we have to create a new string variable and create a getter and setter for that variable. We will be using that variable for storing the id of our document. Below is the code for the updated Courses.java class. Comments are added inside the code to get to know in more detail.
Step 3: Adding onClickListener() for our items of RecyclerView
As we have created our Adapter class in the previous article for displaying the list of courses in Recycler View. In this article, we will add onClickListener() inside that Adapter class for RecyclerView item click listener. Add the following code snippet to the CourseRVAdapter.java file.
Below is the updated code for the CourseRVAdapter.java file.
Step 4: Update code in the CourseDetails.java file
As we have created a new variable inside our modal class for storing document id so we have to initialize that variable and pass our document id inside that variable. For passing that document id we have to update our code for the CourseDetails.java file where we are displaying the list of all the courses which we have added. Add the following code snippet to the CourseRVAdapter.java file.
Below is the updated code for the CourseDetails.java file.
Step 5: Now we will move towards the implementation of our UpdateCourses.java file
After updating our Adapter class, navigate to the app > java > your app’s package name > UpdateCourses.java file and add the below code to it. Comments are added inside the code to understand the code in more detail.
Please Login to comment...