Retriving Data from MySql Database and Saving them in Realm Database in Android

in #utopian-io7 years ago

realmDark.jpg

Repository

https://github.com/realm/realm-java

What Will I Learn?

  • How to transfer data from a MySql database to a Realm database
  • How to pass data from one activity to another using Intents.
  • How to load images in ImageView using Picasso.

Requirements

  • An Integrated Development Environment(IDE) for building Android Application(e.g Anroid Studio, IntelliJ)
  • An Android Device or a Virtual Device.
  • Local Server Installed (e.g Xamp or Wamp)
  • Little PHP and MySql Coding Experience.
  • Java Programming Experience.
  • Of course, willingness to learn

Resources

Dependencies used

  • implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
  • implementation 'com.squareup.picasso:picasso:2.5.2'
  • implementation 'com.squareup.retrofit2:retrofit:2.1.0'
  • implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
  • implementation 'com.squareup.okhttp3:okhttp:3.3.1'

Difficulty

  • Intermediate

Description

Realm Definition and Source

Outline

In this tutorial, we will be developing an android application that will take details from a mysql database and save it in a realm database and then display the data in a recyclerview.

0:00 - 03:04

The MySQL database is explained here.
Explanations such as the intended details the android application will be receiving such as name, image, title, dob (Date of Birth), pob (Place of Birth) and discoveries.

A PHP script (location: C:\xampp\htdocs\profile\getprofiles) contains the code that we use to get all the rows of data contained in our database.

We use some details in order to connect to the database such as the database name - steem, a database user with the necessary privileges - root and user's password, server name - localhost.

We finally call the json_encode method on an array that was initialized with the necessary details and sent it to the application using the echo command.

03:42 - 04:05

Explanation of dependencies used.

04:05 - 06:09

Explanation of the MainActivity layout file (activity_main.xml)

Here the layout code is being explained here, firstly when the user opens the application, he will be welcomed with a screen that just shows a button with the text Get Profiles and upon clicking the button, a request is made to the local server to get the profiles from the server using retrofit.

And in that same layout exists a recyclerview with its visibility set to gone and once the profiles are gotten from the Mysql database and loaded successfully into the recyclerview, the visibility of this recyclerview is set to VISIBLE and then the visibility of the button is set to GONE.

06:12 - 07:49

We generate butterknife injections here for the recyclerview with the id - profiles_recView and also an onCLick method injection for the button with the id - get_profiles.

For butterknife injections for the views in a layout file, you insert your cursor on the setContent view method specifically on the layout resource id - setContentView(R.layout.activity_main); , hit alt + ins keys on a mac and then select butterknife injections.

07:50 - 10:21

The neccesary files needed to make a retrofit call with includes a Client and a Service is explained.

The service file defines the method to be called, how the request would be made (Post or Get Methods) and also parameters that would accompany the request if there be any lastly the result expected if the request is successful.

While the ApiClient defines an OkHttpClient object which specifies the connectTimeout(),readTimeout(),writeTimeout(),retryOnConnectionFailure() properties.
A Gson object is also defined and then a retrofit object is returned with the client being the okHttpClient, a base url which is the url of our local server, a Converter Factory which is our gson object.

10:30 - 16:40

The retrofit methods and requirements are explained here, the onResponse method is used to know if the request was successful and then if it was, we get the result from the response body and then we loop through the response and then save it inside our realm database.

In the while loop, we create a new realObject of the Profiles class and then set the appropriate details of each profile.

And when the user clicks on a recyclerview element, we call the getDetailsAndSend() method which take the realm database reference and also the position of the element that was clicked and then get the neccesary details and send to the DetailsActivity Activity for the display of more details for that selected element.

22:08 - 24:10

The key and value pair of details sent from the MainActivity.java file is gotten and then displayed in the appropriate textviews.

Notables

  • For butterknife injection - alt + ins => Generate butterknife injection.
  • To generate Getter and Setter Methods - alt + ins => Getter and Setter.

Video Tutorial

Application Execution

Related Videos

Creating a Contact Saving Android Application with Realm Database

Tutorial codes can be gotten from this GitHub repository: https://github.com/generalkolo/Realm

Proof of Work

https://github.com/generalkolo

Sort:  

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!

Thank you for your contribution @edetebenezer.

It has been reviewed and scored accordingly.

Here are some guidelines to help you improve on the video tutorial:

  • For video tutorials, you need to work on the structure of your presentation.

  • Try to draft out an outline of your tutorial so that you have a structured plan as you teach your audience the concepts.

  • Video tutorials need to be engaging for the audience. You can take a look at this example for your reference:

https://steemit.com/utopian-io/@buckydurddle/vcv-rack-valley-s-dexter-part-1


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.

[utopian-moderator]

Thanks @rosatravels I just watched the video from @buckydurddle.
I will sure have that draft in my subsequent contributions and make them engaging for the users and lastly thank you for taking out time to moderate my contribution.

You have a minor misspelling in the following sentence:

The neccesary files needed to make a retrofit call with includes a Client and a Service is explained.
It should be necessary instead of neccesary.