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.Â
XML
| <?xmlversion="1.0"encoding="utf-8"?><LinearLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".UpdateCourse">      <!--Edittext for getting course Name-->    <EditText        android:id="@+id/idEdtCourseName"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginStart="10dp"        android:layout_marginTop="20dp"        android:layout_marginEnd="10dp"        android:hint="Course Name"        android:importantForAutofill="no"        android:inputType="text"/>      <!--Edittext for getting course Duration-->    <EditText        android:id="@+id/idEdtCourseDuration"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginStart="10dp"        android:layout_marginTop="20dp"        android:layout_marginEnd="10dp"        android:hint="Course Duration in min"        android:importantForAutofill="no"        android:inputType="time"/>      <!--Edittext for getting course Description-->    <EditText        android:id="@+id/idEdtCourseDescription"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginStart="10dp"        android:layout_marginTop="20dp"        android:layout_marginEnd="10dp"        android:hint="Course Description"        android:importantForAutofill="no"        android:inputType="text"/>        <!--Button for updating your course to Firebase-->    <Button        android:id="@+id/idBtnUpdateCourse"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:text="Update Course Details"        android:textAllCaps="false"/>  </LinearLayout> | 
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.Â
Java
| importcom.google.firebase.firestore.Exclude;  importjava.io.Serializable;  // we have to implement our modal class// with serializable so that we can pass// our object class to new activity on// our item click of recycler view.publicclassCourses implementsSerializable {      // getter method for our id    publicString getId() {        returnid;    }      // setter method for our id    publicvoidsetId(String id) {        this.id = id;    }      // we are using exclude because    // we are not saving our id    @Exclude    privateString id;    Â    // variables for storing our data.    privateString courseName, courseDescription, courseDuration;      publicCourses() {        // empty constructor required for Firebase.    }      // Constructor for all variables.    publicCourses(String courseName, String courseDescription, String courseDuration) {        this.courseName = courseName;        this.courseDescription = courseDescription;        this.courseDuration = courseDuration;    }      // getter methods for all variables.    publicString getCourseName() {        returncourseName;    }      publicvoidsetCourseName(String courseName) {        this.courseName = courseName;    }      publicString getCourseDescription() {        returncourseDescription;    }      // setter method for all variables.    publicvoidsetCourseDescription(String courseDescription) {        this.courseDescription = courseDescription;    }      publicString getCourseDuration() {        returncourseDuration;    }      publicvoidsetCourseDuration(String courseDuration) {        this.courseDuration = courseDuration;    }} | 
 Â
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.
Java
| // here we are adding on click listener// for our item of recycler view.itemView.setOnClickListener(newView.OnClickListener() {                @Override                publicvoidonClick(View v) {                  Â                    // after clicking of the item of recycler view.                    // we are passing our course object to the new activity.                    Courses courses = coursesArrayList.get(getAdapterPosition());                      // below line is creating a new intent.                    Intent i = newIntent(context, UpdateCourse.class);                      // below line is for putting our course object to our next activity.                    i.putExtra("course", courses);                      // after passing the data we are starting our activity.                    context.startActivity(i);           }    }); | 
Below is the updated code for the CourseRVAdapter.java file.
Java
| importandroid.content.Context;importandroid.content.Intent;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.TextView;  importandroidx.annotation.NonNull;importandroidx.recyclerview.widget.RecyclerView;  importjava.util.ArrayList;  publicclassCourseRVAdapter extendsRecyclerView.Adapter<CourseRVAdapter.ViewHolder> {    // creating variables for our ArrayList and context    privateArrayList<Courses> coursesArrayList;    privateContext context;      // creating constructor for our adapter class    publicCourseRVAdapter(ArrayList<Courses> coursesArrayList, Context context) {        this.coursesArrayList = coursesArrayList;        this.context = context;    }      @NonNull    @Override    publicCourseRVAdapter.ViewHolder onCreateViewHolder(@NonNullViewGroup parent, intviewType) {        // passing our layout file for displaying our card item        returnnewViewHolder(LayoutInflater.from(context).inflate(R.layout.course_item, parent, false));    }      @Override    publicvoidonBindViewHolder(@NonNullCourseRVAdapter.ViewHolder holder, intposition) {        // setting data to our text views from our modal class.        Courses courses = coursesArrayList.get(position);        holder.courseNameTV.setText(courses.getCourseName());        holder.courseDurationTV.setText(courses.getCourseDuration());        holder.courseDescTV.setText(courses.getCourseDescription());    }      @Override    publicintgetItemCount() {        // returning the size of our array list.        returncoursesArrayList.size();    }      classViewHolder extendsRecyclerView.ViewHolder {        // creating variables for our text views.        privatefinalTextView courseNameTV;        privatefinalTextView courseDurationTV;        privatefinalTextView courseDescTV;          publicViewHolder(@NonNullView itemView) {            super(itemView);            // initializing our text views.            courseNameTV = itemView.findViewById(R.id.idTVCourseName);            courseDurationTV = itemView.findViewById(R.id.idTVCourseDuration);            courseDescTV = itemView.findViewById(R.id.idTVCourseDescription);              // here we are adding on click listener            // for our item of recycler view.            itemView.setOnClickListener(newView.OnClickListener() {                @Override                publicvoidonClick(View v) {                  Â                    // after clicking of the item of recycler view.                    // we are passing our course object to the new activity.                    Courses courses = coursesArrayList.get(getAdapterPosition());                      // below line is creating a new intent.                    Intent i = newIntent(context, UpdateCourse.class);                      // below line is for putting our course object to our next activity.                    i.putExtra("course", courses);                      // after passing the data we are starting our activity.                    context.startActivity(i);                }            });        }    }} | 
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.
Java
| // below is the updated line of code which we have to// add to pass the document id inside our modal class.// we are setting our document id with d.getId() methodc.setId(d.getId()); | 
Below is the updated code for the CourseDetails.java file.
Java
| importandroid.os.Bundle;importandroid.view.View;importandroid.widget.ProgressBar;importandroid.widget.Toast;  importandroidx.annotation.NonNull;importandroidx.appcompat.app.AppCompatActivity;importandroidx.recyclerview.widget.LinearLayoutManager;importandroidx.recyclerview.widget.RecyclerView;  importcom.google.android.gms.tasks.OnFailureListener;importcom.google.android.gms.tasks.OnSuccessListener;importcom.google.firebase.firestore.DocumentSnapshot;importcom.google.firebase.firestore.FirebaseFirestore;importcom.google.firebase.firestore.QuerySnapshot;  importjava.util.ArrayList;importjava.util.List;  publicclassCourseDetails extendsAppCompatActivity {      // creating variables for our recycler view,    // array list, adapter, firebase firestore    // and our progress bar.    privateRecyclerView courseRV;    privateArrayList<Courses> coursesArrayList;    privateCourseRVAdapter courseRVAdapter;    privateFirebaseFirestore db;    ProgressBar loadingPB;      @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_course_details);          // initializing our variables.        courseRV = findViewById(R.id.idRVCourses);        loadingPB = findViewById(R.id.idProgressBar);          // initializing our variable for firebase        // firestore and getting its instance.        db = FirebaseFirestore.getInstance();          // creating our new array list        coursesArrayList = newArrayList<>();        courseRV.setHasFixedSize(true);        courseRV.setLayoutManager(newLinearLayoutManager(this));          // adding our array list to our recycler view adapter class.        courseRVAdapter = newCourseRVAdapter(coursesArrayList, this);          // setting adapter to our recycler view.        courseRV.setAdapter(courseRVAdapter);          // below line is use to get the data from Firebase Firestore.        // previously we were saving data on a reference of Courses        // now we will be getting the data from the same reference.        db.collection("Courses").get()                .addOnSuccessListener(newOnSuccessListener<QuerySnapshot>() {                    @Override                    publicvoidonSuccess(QuerySnapshot queryDocumentSnapshots) {                        // after getting the data we are calling on success method                        // and inside this method we are checking if the received                        // query snapshot is empty or not.                        if(!queryDocumentSnapshots.isEmpty()) {                            // if the snapshot is not empty we are                            // hiding our progress bar and adding                            // our data in a list.                            loadingPB.setVisibility(View.GONE);                            List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();                            for(DocumentSnapshot d : list) {                                // after getting this list we are passing                                // that list to our object class.                                Courses c = d.toObject(Courses.class);                                  // below is the updated line of code which we have to                                // add to pass the document id inside our modal class.                                // we are setting our document id with d.getId() method                                c.setId(d.getId());                                  // and we will pass this object class                                // inside our arraylist which we have                                // created for recycler view.                                coursesArrayList.add(c);                            }                            // after adding the data to recycler view.                            // we are calling recycler view notifyDataSetChanged                            // method to notify that data has been changed in recycler view.                            courseRVAdapter.notifyDataSetChanged();                        } else{                            // if the snapshot is empty we are displaying a toast message.                            Toast.makeText(CourseDetails.this, "No data found in Database", Toast.LENGTH_SHORT).show();                        }                    }                }).addOnFailureListener(newOnFailureListener() {            @Override            publicvoidonFailure(@NonNullException e) {                // if we do not get any data or any error we are displaying                // a toast message that we do not get any data                Toast.makeText(CourseDetails.this, "Fail to get the data.", Toast.LENGTH_SHORT).show();            }        });    }} | 
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.
Java
| importandroid.os.Bundle;importandroid.text.TextUtils;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.Toast;  importandroidx.annotation.NonNull;importandroidx.appcompat.app.AppCompatActivity;  importcom.google.android.gms.tasks.OnFailureListener;importcom.google.android.gms.tasks.OnSuccessListener;importcom.google.firebase.firestore.FirebaseFirestore;  publicclassUpdateCourse extendsAppCompatActivity {      // creating variables for our edit text    privateEditText courseNameEdt, courseDurationEdt, courseDescriptionEdt;      // creating a strings for storing our values from Edittext fields.    privateString courseName, courseDuration, courseDescription;      // creating a variable for firebasefirestore.    privateFirebaseFirestore db;      @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_update_course);        Courses courses = (Courses) getIntent().getSerializableExtra("course");          // getting our instance from Firebase Firestore.        db = FirebaseFirestore.getInstance();          // initializing our edittext and buttons        courseNameEdt = findViewById(R.id.idEdtCourseName);        courseDescriptionEdt = findViewById(R.id.idEdtCourseDescription);        courseDurationEdt = findViewById(R.id.idEdtCourseDuration);          // creating variable for button        Button updateCOurseBtn = findViewById(R.id.idBtnUpdateCourse);          courseNameEdt.setText(courses.getCourseName());        courseDescriptionEdt.setText(courses.getCourseDescription());        courseDurationEdt.setText(courses.getCourseDuration());            updateCOurseBtn.setOnClickListener(newView.OnClickListener() {            @Override            publicvoidonClick(View v) {                courseName = courseNameEdt.getText().toString();                courseDescription = courseDescriptionEdt.getText().toString();                courseDuration = courseDurationEdt.getText().toString();                  // validating the text fields if empty or not.                if(TextUtils.isEmpty(courseName)) {                    courseNameEdt.setError("Please enter Course Name");                } elseif(TextUtils.isEmpty(courseDescription)) {                    courseDescriptionEdt.setError("Please enter Course Description");                } elseif(TextUtils.isEmpty(courseDuration)) {                    courseDurationEdt.setError("Please enter Course Duration");                } else{                    // calling a method to update our course.                    // we are passing our object class, course name,                    // course description and course duration from our edittext field.                    updateCourses(courses, courseName, courseDescription, courseDuration);                }            }        });    }      privatevoidupdateCourses(Courses courses, String courseName, String courseDescription, String courseDuration) {        // inside this method we are passing our updated values        // inside our object class and later on we        // will pass our whole object to firebase Firestore.        Courses updatedCourse = newCourses(courseName, courseDescription, courseDuration);          // after passing data to object class we are        // sending it to firebase with specific document id.        // below line is use to get the collection of our Firebase Firestore.        db.collection("Courses").                // below line is use toset the id of                // document where we have to perform                // update operation.                document(courses.getId()).                  // after setting our document id we are                // passing our whole object class to it.                set(updatedCourse).                  // after passing our object class we are                // calling a method for on success listener.                addOnSuccessListener(newOnSuccessListener<Void>() {                    @Override                    publicvoidonSuccess(Void aVoid) {                        // on successful completion of this process                        // we are displaying the toast message.                        Toast.makeText(UpdateCourse.this, "Course has been updated..", Toast.LENGTH_SHORT).show();                    }                }).addOnFailureListener(newOnFailureListener() {            // inside on failure method we are            // displaying a failure message.            @Override            publicvoidonFailure(@NonNullException e) {                Toast.makeText(UpdateCourse.this, "Fail to update the data..", Toast.LENGTH_SHORT).show();            }        });    }} | 


 
                                    







