React Native Show Realm Database Data into ListView Android iOS Example Tutorial

As we have discussed in our previous tutorials about Inserting Data into Realm database, Now this tutorial is the next part of inserting data tutorial. Inside this tutorial we would going to perform some amazing functionality and Display all the Already inserted items into Realm Mobile Database inside a Custom ListView. We would render all the items in a group one by one and they would render on the mobile phone screen. So in this tutorial we would going to Show Realm Database Data into ListView in react native Android iOS app Example Tutorial.

What we are doing in this project:

  • We are using the React Navigation library in our project in order to use the Activity structure because there are two activities in this project. 1st one is MainActivity and 2nd is ShowDataActivity.
  • We have to add the Realm mobile database library in our project in order to use the Realm database.

Activities in present project:

  1. MainActivity
  2. ShowDataActivity

Contents in this project Show Realm Database Data into ListView Android iOS React Native App Example Tutorial:

1. We need to install the React Navigation library in our project because we are using multiple activities in our current project. To install the React Navigation library in your project Open your react native project folder path in CMD(Command Prompt) and execute the below command.

Screenshot of CMD :


Screenshot of CMD after successfully installed library:

2. Now we need to install the Realm library in order to use Realm Mobile Database into our project. To install realm in your project again open your react native’s project folder into CMD(Command Prompt) and execute below command.

Screenshot of CMD:

Screenshot of CMD after successfully installing this library:

3. After successfully installing the library we need to run 
reactnative link realm command so the newly installed Realm libraries and dependencies would index in our project.

Screenshot of CMD after executing above command:

4. Open your project’s App.js file and import StyleSheet, Platform, View, Image, Text, TextInput, TouchableOpacity, Alert, YellowBox and ListView component in your project.

5. Import the Realm in your project and create a global variable name as realm. We would use the realm using this variable.

6. Import the StackNavigator module from react navigation library to use multiple activities in your project.

7. Creating a activity class named as MainActivity in project. Inside this activity we have explaining about Creating Realm database and Creating tables inside it and inserting data from TextInput to realm database. So i have already make a tutorial on this topic. If you want to understand all inserting process is working you can read my this tutorial about Insert Data into Realm database here.

Screenshot:

8. Creating another class named as ShowDataActivity in your project. This would be our displaying data class.

9. Creating Constructor() in ShowDataActivity class.

YellowBox : Used to hide the componentWillMount() yellow box errors, which is currently coming with every newly build react native project.

mydata : Variable object holds all the data from Realm database.

ds : Creating ListView data source row changed.

dataSource : Creating a State named as dataSource and setting up all the Realm data into dataSource State.

10. Creating a function named as GetClickedItem() inside ShowDataActivity, This function would display the Selected item from ListView.

11. Creating a function named as ListViewItemSeparator(), This function would render a Horizontal line after printing each element of ListView.

12. Create ListView component inside render’s return block in ShowDataActivity and Show Realm Database Data into ListView one by one.

dataSource : Setting up the data source in this prop.

renderSeparator : Calling the item separator function.

13. Complete Code of ShowDataActivity class file.

Screenshot:

Show Realm Database Data into ListView

14. Creating StackNavigator activity navigation structure to implement and control between multiple activities.

15. Creating Styles.

16. Complete source code for App.js File :

Screenshots in Android device:

Show Realm Database Data into ListView Screenshots in iOS device: