June 16, 2021

How to load data from remote database to android recyclerview

This is done using REST APIs. We described in detail about these REST API in our earlier tutorial. Here, let us discuss how to use them in an android project.

Here, we are going to load data in remote MySQL database into our android app. We use recyclerviews for this.

First of all, we need to create the backend REST APIs. I use Laravel framework for this. It follows the MVC architecture. We’ll discuss MVC architecture in another tutorial. For now, we can say it is a concept with three components namely, Model, View and Controller. We implement our REST API within a controller. With the below command we can create our controller.

php artisan make:controller AppController

Here, php artisan make:controller is the static part of the command and ‘AppController’ is the name I gave to my controller.

Then our controller is ready. Here is the code we implement within the controller. We can implement a method like this. It will then query out and return data in json format. ‘data’ is the name of the database table.

Then go to routes>>api.php. There we add the route like this.

The first parameter here is known as the endpoint. The second parameter contains the method name followed by the Controller name.

We have finished creating our REST API. Now we are going to create our android project.

Open android studio and start a new project like this.

After you create the project add the retrofit, gson converter, RecyclerView dependancies into the build.gradle (Module: app)

Then sync the project and there after all the necessary dependancies will be downloaded.

After that create the java class containing the getters and setters. Here we name it as Months.java

Then let us create the Interface to define the endpoints. In this sample app, we have only one endpoint named ‘/data’ .

Here we are going to get this data into recyclerviews. For that we need to bind the data coming from the API call. That is done using an adapter class. We’ll create that adapter class as DataAdapter.java

Next, we need to create a new class with a retrofit instance. We provide a BASE_URL which is relevant to the path of the implemented REST API. This class then is responsible for sending network requests to REST APIs using Retrofit.

Here when you provide the BASE_URL, if the database is in your local machine as in mine, use the current IP address and then the path to your Laravel project in htdocs. For me it is, Project>>MyTest1. MyTest1 is the name of my Laravel project. Then inside it we provide the path to our API, which is always ‘public/api/’

This is the layout file for showing data in RecyclerView using CardViews.

Next we create the MainActivity.java which is the main java file of our project. This contains the API call. The method enqueue() sends the request and notifies our app when a response is received. And also this handles what to do if request is failed. For that we implement two callback methods as onResponse() and onFailure().

Then this is the xml file with the recyclerview for the data. It is in the activity_main.xml file.

Now we have finished many parts of our app. Let us add the internet permissions in the AndroidManifest.xml file.

Now we have finished the implementation part of our app. Run it on an emulator or an android device. Then you can see how it is working….:)

54 thoughts on “How to load data from remote database to android recyclerview

  1. Greetings I am so happy I found your webpage, I really found you by
    accident, while I was looking on Aol for something else, Anyhow I
    am here now and would just like to say thanks a lot for a tremendous
    post and a all round exciting blog (I also love the theme/design), I don’t
    have time to go through it all at the minute but I have saved it and
    also added your RSS feeds, so when I have time I will be
    back to read a great deal more, Please do keep up the excellent jo.

  2. I truly love your site.. Great colors & theme. Did you make this web site yourself?

    Please reply back as I’m planning to create
    my own site and would love to find out where you got this from or what
    the theme is named. Thank you!

  3. Hey I know this is off topic but I was wondering if you knew of any widgets I could add
    to my blog that automatically tweet my newest twitter
    updates. I’ve been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading
    your blog and I look forward to your new updates.

  4. I do not know whether it’s just me or if everyone else encountering problems
    with your site. It seems like some of the written text in your posts are running off the screen. Can someone else please comment and
    let me know if this is happening to them as well? This may be a problem
    with my browser because I’ve had this happen before.

  5. My partner and I stumbled over here from a different web
    page and thought I should check things out.
    I like what I see so now i am following you. Look forward to
    exploring your web page again.

  6. Simply wish to say your article is as amazing.
    The clarity to your put up is simply excellent and i can assume
    you’re an expert on this subject. Well together with your
    permission let me to grasp your RSS feed to stay up to date with drawing close post.
    Thanks one million and please continue the rewarding work.

  7. Good day! This is my first comment here so I just wanted to give a quick shout out and say I truly enjoy reading your blog posts.
    Can you recommend any other blogs/websites/forums that go over the same topics?

  8. My programmer is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using WordPress on various websites for about a year and am anxious about
    switching to another platform. I have heard great things
    about blogengine.net. Is there a way I can transfer all my wordpress posts into it?
    Any help would be greatly appreciated!

  9. First off I would like to say fantastic blog! I had a quick question in which I’d
    like to ask if you don’t mind. I was interested
    to find out how you center yourself and clear your
    mind prior to writing. I’ve had a hard time clearing my mind
    in getting my ideas out. I do take pleasure in writing
    but it just seems like the first 10 to 15 minutes tend
    to be wasted just trying to figure out how to
    begin. Any recommendations or hints? Cheers!

  10. Please let me know if you’re looking for a
    author for your blog. You have some really great articles and I think I would be a good asset.
    If you ever want to take some of the load off, I’d love to write some material for your blog in exchange for a link back to mine.
    Please shoot me an email if interested. Thank you!

  11. What’s Taking place i’m new to this, I stumbled upon this I’ve discovered It
    absolutely helpful and it has aided me out loads. I’m hoping to contribute & help other users like its aided me.
    Great job.

  12. I am not sure where you’re getting your info, however good topic.
    I needs to spend some time finding out much more or working out more.
    Thank you for fantastic information I used to be searching
    for this information for my mission.

  13. Nice blog here! Also your website loads up fast! What web host are you using?
    Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol

  14. It’s perfect time to make some plans for the future and it’s time to be happy.
    I have read this post and if I could I want to suggest you few
    interesting things or tips. Maybe you could write next articles referring to this article.
    I desire to read even more things about it!

  15. I blog quite often and I seriously thank you for your information. This great article has really peaked
    my interest. I’m going to book mark your website and keep checking for new
    details about once per week. I subscribed to your Feed too.

  16. I do not even understand how I stopped up right here, however I assumed this put up was good.
    I do not realize who you’re but definitely you are going to a well-known blogger should you
    are not already. Cheers!

  17. Nice blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements would
    really make my blog shine. Please let me know where you got your design.
    Many thanks

  18. An intriguing discussion is definitely worth comment. I believe that you need to publish more on this
    issue, it might not be a taboo matter but usually folks don’t
    speak about these subjects. To the next! All the best!!

  19. We are a gaggle of volunteers and opening a new scheme
    iin our community. Your website offeed us with helpful information to work on. You have done a formidable activity and our
    whole grdoup will likely be thaqnkful to you.

    Here is my web-site; Indian news

  20. Great post. I used to be checking constantly this weblog and I’m impressed! Very helpful information specially the final part 🙂 I take care of such information much. I was seeking this particular info for a very lengthy time. Thank you and best of luck.

  21. What’s up i am kavin, its my first occasion to commenting anyplace,
    when i read this piece of writing i thought i could also make comment due to this brilliant article.

Leave a Reply

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