Creating a Realm Database and Populating it using JSON File in Android
Repository
https://github.com/realm/realm-java
Retrofit Image
What Will I Learn?
- How to create a Realm Objects and save to it using JSON File
- How to use the Lombok plugin.
- How to use the RealmRecyclerViewAdapter and its subclasses.
- How to use the InputStream to read from raw files.
Requirements
- An Integrated Development Environment(IDE) for building Android Application(e.g Anroid Studio, IntelliJ)
- An Android Device or a Virtual Device.
- Java Programming Experience.
- Of course, willingness to learn
Resources
- Retrofit Website. https://realm.io/
- Retrofit Github. - https://github.com/realm
- Retrofit License - Apache License
Difficulty
- Intermediate
Description
Tutorial Overview
In this tutorial, we are going to be learning on how to create a realm database and populate the database using a json
file and in order to teach this, we are going to be creating a fruit list application which will be using a recyclerview to show the fruit Image, Name and Price.
In computing, JavaScript Object Notation or JSON is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value). It is a very common data format used for asynchronous browser–server communication, including as a replacement for XML in some AJAX-style systems.
Source - https://en.wikipedia.org/wiki/JSON
We are also going to be using the Lombok plugin/library (https://projectlombok.org) to inject Getter and Setter methods using the @Getter
and @Setter
annotations.
Outline
User Dependencies (01:13 - 02:33)
- implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
The Butterknife plugin is used to make the injection of views in our activity java class file easier and simpler.
All you have to do is insert your cursor on the layout file then press alt + ins
on a windows pc , select the Generate Butterknife Injection
option and then select the views (Edittext, TextView , Button etc) and then click the confirm button.
- implementation 'io.realm:android-adapters:2.1.0'
The relam adapter plugin makes it possible that we use the RealmRecyclerViewAdapter
instead of the default RecyclerViewAdapter
which makes databinding a lot easier and less stresful as you will see in the tutorial.
(09:20 - 18:31)
- implementation 'org.projectlombok:lombok:1.16.20'
annotationProcessor 'org.projectlombok:lombok:1.16.20'
The lombok library makes the creation of getter and setter methods easier just by including the @Getter
and @Setter
annotations on the fields. (07:38 - 09:07)
- implementation 'com.squareup.picasso:picasso:2.71828'
The Picasso plugin/library is used to load images.
Including RecyclerView into xml and Creating Single row Layout (02:35 - 05:53)
Inorder to display the list of fruits, we create a recycler view that will fill the screen by setting its height
and weight
properties to match_parent
with an id of fruitsRecView
. The id
is useful for us when we inject the recyclerview in our activity java class file using ButterKnife
.
Also, to display the list of fruits inside the recycler view, we must create a layout file that will determine how each list will look.
Inorder to achieve the above layout, we create a new layout resource file by right clicking on our res folder and selecting new resource file as shown in the image below.
We insert three views - ImageView
, and then two TextViews
which will be used to display the image of the Fruit and the price and name.
Creating JSON file in raw
android repository Folder (05:53 - 07:46)
Inorder to create our json file, we have to save it in the raw
android repository folder, the JSON file contains Five JSON objects with three fields - Image
, Name
, Price
which will as the name implies display the Image, Price and Name of each fruit.
Creation Of Model Class (07:46 - 09:07)
For us to correctly be able to display the details of the fruits from the JSON file, we must have an appropraite model class that will be used to get
and set
the details of the fruits.
The Fruits model is created and since we are intending to use it as our realm class, it must extend the RealmObject
class.
We finally use the lombok library to set the gettr and setter methods of the fields.
Creating our custom Adapter (09:22 - 18:31)
To bind the views from the single_fruit_row
into our recyclerview, we create a custom Adapter - FruitsAdapter
that extends from the RealmRecyclerViewAdapter
where we use the onCreateViewHolder()
method to inflate the single_fruit_row
layout file and also use the onBindViewHolder()
method to set the neccesary details of each row of fruits.
The FruitsViewHolder
class uses ButterKnife
to bind the views in the single_fruit_row
so that they can be accessable in the onBindViewHolder()
method.
Linking Everything Together (18:43 - 24:23)
In our MainActivity.java class file, we create the Realm database and populate it in our fillUpDatabase()
method where we use an InputStream
variable that opens a Raw resource file which is our JSON file (fruits.json).
We then create the realm objects from the json file using the createAllFromJson()
method and passing the Fruits
model as the first parameter and the inputStream
variable as the second arguement.
Next we get all the objects of Fruits
type from the realm database using the realm.where().findAll()
method store in a RealmResults
variable and pass this to our custom Adapter and then set a vertical LinearLayoutManager as the layout manager of the recycler view and then set its fix size - .setHasFixedSize()
to true.
Video Tutorial
Application Execution
Related Videos
Creating a Contact Saving Android Application with Realm Database
- Creating a Contact Saving Android Application with Realm Database - PART 1
- Creating a Contact Saving Android Application with Realm Database - PART 2
- Creating a Contact Saving Android Application with Realm Database - PART 3
- Creating a Contact Saving Android Application with Realm Database - PART 4
Hey @edetebenezer
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
The contribution is reviewed and scored accordingly.
Link to the Answers of the Questionnaire -
Click here
@edetebenezer, here are some guidelines for you to improve on your contribution:
video tutorials need to be structured with outlines so that the learners can follow your steps one to another to stay engaged, or else you will lose your learners easily.
try to edit your video after it is recorded. There are a lot of distracting background noises. You can use the audacity program to edit the noise out.
You can also edit your video with pointers and boxes to show learners where to focus as they follow your video presentations.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Thanks @rosatravels corrections will be made accordingly.
You have a minor misspelling in the following sentence:
It should be argument instead of arguement.