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  react-native 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:

15 Comments

  1. Hi.
    Thank you for all tutorials on Realm. Please, continue and maybe after Realm, react native and firebase. 😉

  2. Amazing stuff :). Thanx for sharing !

    • Welcome Irbe 🙂 .

      • Hi admin, Thanks for the great tutorial on realm database. I’m also new to this one. I have one query. How can access schema object created in one component into another component?

        It’s like I have created add_user() in one component on one of my sceen and inserted data into schema. But again in another screen I have to insert the data into same schema. How can I do this ?

  3. Nocode Developer

    its great for me sir, thankyou for sharing your knowledge…

  4. React Developer

    Hi admin Great Work!!! Can You just add Functionality of Edit and Delete to Same Project.

  5. Sir I need help when I enter employee id to the text input field than I want to display full name and id of employee to the second screen.

  6. Hello sir,
    I am getting error while installing realm using this command
    “npm install –save realm”

    C:\Users\Maan\Desktop\Demo>npm install –save realm
    > [email protected].0 install C:\Users\Maan\Desktop\Demo\node_modules\realm
    > node-pre-gyp install –fallback-to-build

    node-pre-gyp ERR! Tried to download(404): https://static.realm.io/node-pre-gyp/2
    .13.0/realm-v2.13.0-node-v64-win32-x64.tar.gz
    node-pre-gyp ERR! Pre-built binaries not found for [email protected].0 and [email protected]
    (node-v64 ABI, unknown) (falling back to source compile with node-gyp)
    node-pre-gyp ERR! Tried to download(undefined): https://static.realm.io/node-pre
    -gyp/2.13.0/realm-v2.13.0-node-v64-win32-x64.tar.gz
    node-pre-gyp ERR! Pre-built binaries not found for [email protected].0 and [email protected]
    (node-v64 ABI, unknown) (falling back to source compile with node-gyp)
    gyp ERR! configure error
    gyp ERR! stack Error: Can’t find Python executable “python”, you can set the PYT
    HON env variable.
    gyp ERR! stack at PythonFinder.failNoPython (C:\Users\Maan\AppData\Roaming\n
    pm\node_modules\npm\node_modules\node-gyp\lib\configure.js:483:19)
    gyp ERR! stack at PythonFinder. (C:\Users\Maan\AppData\Roaming\np
    m\node_modules\npm\node_modules\node-gyp\lib\configure.js:508:16)
    gyp ERR! stack at C:\Users\Maan\AppData\Roaming\npm\node_modules\npm\node_mo
    dules\graceful-fs\polyfills.js:284:29
    gyp ERR! stack at FSReqWrap.oncomplete (fs.js:158:21)
    gyp ERR! System Windows_NT 6.1.7601
    gyp ERR! command “C:\\Program Files\\nodejs\\node.exe” “C:\\Users\\Maan\\AppData
    \\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js” “co
    nfigure” “–fallback-to-build” “–module=C:\\Users\\Maan\\Desktop\\Demo\\node_mo
    dules\\realm\\compiled\\node-v64_win32_x64\\realm.node” “–module_name=realm” “-
    -module_path=C:\\Users\\Maan\\Desktop\\Demo\\node_modules\\realm\\compiled\\node
    -v64_win32_x64”
    gyp ERR! cwd C:\Users\Maan\Desktop\Demo\node_modules\realm
    gyp ERR! node -v v10.6.0
    gyp ERR! node-gyp -v v3.6.2
    gyp ERR! not ok
    node-pre-gyp ERR! build error
    node-pre-gyp ERR! stack Error: Failed to execute ‘C:\Program Files\nodejs\node.e
    xe C:\Users\Maan\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\bin\
    node-gyp.js configure –fallback-to-build –module=C:\Users\Maan\Desktop\Demo\no
    de_modules\realm\compiled\node-v64_win32_x64\realm.node –module_name=realm –mo
    dule_path=C:\Users\Maan\Desktop\Demo\node_modules\realm\compiled\node-v64_win32_
    x64’ (1)
    node-pre-gyp ERR! stack at ChildProcess. (C:\Users\Maan\Desktop\D
    emo\node_modules\node-pre-gyp\lib\util\compile.js:83:29)
    node-pre-gyp ERR! stack at ChildProcess.emit (events.js:182:13)
    node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:961:16)
    node-pre-gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/chi
    ld_process.js:248:5)
    node-pre-gyp ERR! System Windows_NT 6.1.7601
    node-pre-gyp ERR! command “C:\\Program Files\\nodejs\\node.exe” “C:\\Users\\Maan
    \\Desktop\\Demo\\node_modules\\node-pre-gyp\\bin\\node-pre-gyp” “install” “–fal
    lback-to-build”
    node-pre-gyp ERR! cwd C:\Users\Maan\Desktop\Demo\node_modules\realm
    node-pre-gyp ERR! node -v v10.6.0
    node-pre-gyp ERR! node-pre-gyp -v v0.6.39
    node-pre-gyp ERR! not ok
    Failed to execute ‘C:\Program Files\nodejs\node.exe C:\Users\Maan\AppData\Roamin
    g\npm\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js configure –fallbac
    k-to-build –module=C:\Users\Maan\Desktop\Demo\node_modules\realm\compiled\node-
    v64_win32_x64\realm.node –module_name=realm –module_path=C:\Users\Maan\Desktop
    \Demo\node_modules\realm\compiled\node-v64_win32_x64’ (1)
    npm WARN [email protected] requires a peer of [email protected]^3.17.0 || ^
    4.0.0 but none is installed. You must install peer dependencies yourself.
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fse
    vents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]
    1.2.4: wanted {“os”:”darwin”,”arch”:”any”} (current: {“os”:”win32″,”arch”:”x64″}
    )

    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! [email protected].0 install: node-pre-gyp install --fallback-to-build
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the [email protected].0 install script.
    npm ERR! This is probably not a problem with npm. There is likely additional log
    ging output above.

    npm ERR! A complete log of this run can be found in:
    npm ERR! C:\Users\Maan\AppData\Roaming\npm-cache\_logs\2018-07-13T12_54_51_1
    36Z-debug.log

Leave a Reply

Your email address will not be published. Required fields are marked *