In the previous article, we have seen on How to Add Data to Firebase Firestore in Android. This is the continuation of this series. Now we will see How to Read this added data inside our Firebase Firestore. Now we will move towards the implementation of this reading data in Android Firebase.
What we are going to build in this article?
We will be creating a new screen in the previous application and inside that, we will display our data which we added inside our Firebase Firestore in the RecyclerView. We will read all data from Firebase Firestore inside our app.
Step by Step Implementation
Step 1: Working with the activity_main.xml file
Go to the activity_main.xml file add one more Button for showing the list of all added courses. Below is the code snippet and add the code at last.
Now below is the updated code for the activity_main.xml file after adding the above code snippet.
Step 2: Now we will create new Activity for displaying our data from Firebase Firestore in RecyclerView
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 CourseDetails and create new Activity. Make sure to select the empty activity.
Step 3: Now we will move towards the implementation of XML in our Course Details activity
Navigate to the app > res > layout > activity_course_details.xml and add the below code to it.
Step 4: Now we will create a card layout for our item of RecyclerView
To create a new item for RecyclerView, navigate to the app > res > layout > Right-click on layout > New > layout resource file and name as course_item.xml and add below code to it.
Step 5: Now we will create our Adapter class which will handle data for our RecyclerView items
For creating an Adapter class for our Recycler View, navigate to the app > java > Your app’s package name > Right-click on it and Click > New > Java class and name your class as CourseRVAdapter. After creating this class add the below code to it. Comments are added in the code to know in more detail.
Step 6: Working with the CourseDetails.java file
After creating a new Adapter class for our RecyclerView, we have to move towards our CourseDetails.java file and add the below code to it.
Step 7: Working with the MainActivity.java file
In the MainActivity.java file, we have to add an Intent to the CourseDetails.class file. Below is the code snippet to do so.
Below is the updated code for the MainActivity.java file.
After completing this process we have to add the data from our app in Firebase Console and after that click on View Added Courses button you will get to see the list of courses that we have added inside our Firebase Console. Below is the image of how our Firebase Firestore database will look like for only one recycler item.
After completing this Run your app and see the output of the code.