We have seen adding data to the Back4App database in the Android app. In this article, we will take a look at reading this data from our database in our Android App.
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 Back4App Database in the RecyclerView.
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 HomeActivity and create new Activity. Make sure to select the empty activity.
Step 3: Creating a modal class for storing our data.
Navigate to the app > java > your app’s package name > Right-click on it > New > java class and name it as CourseModal and add the below code to it. Comments are added in the code to get to know in more detail.
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_rv_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 these class add the below code to it. Comments are added in the code to know in more detail.
Step 6: Working with the activity_home.xml file
Navigate to the app > res > layout > activity_home.xml file and add the below code to it.
Step 7: Working with the HomeActivity.java file
Navigate to app > java > your app’s package name > HomeActivity.java file and add the below code to it.
Step 8: Working with the MainActivity.java file
In the MainActivity.java file, we have to add an Intent to the HomeActivity file. Below is the code snippet to do so.
Below is the updated code for the MainActivity.java file.
Now run your app and see the output of the app.
Below is the file structure in Android Studio after performing the Read operation:
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.