Android Kotlin FULL COURSE Retrofit2, MVVM, Country List on RecyclerView

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to my channel in today's video i'm going to show you how you can display a country list on a recycler view will make the api call using the retrofit with the help of viewmodel and then will download the list and display it on the recyclerview so let's begin by adding the dependencies in our gradle file so first of all we need to add few dependencies like release retrofit and when we add retrofit so i am using retrofit tools and when we add retrofit along with that we need to add the converter json and this dependency i'm adding so usually i use glide library but this is the extension of that and with the help of this library will be able to get the svg files so normally glide and support svg so i'm going to use this library and when i said i'm going to use this library so we need to add another repository that is the maven and https.jetpack.io inside our project level build.gradle file first of all i am adding another package for our network classes so inside this retro package i am creating a file that is for the retro instance so this class will basically having a companion object companion object is similar to the static in java and this companion object will having a base url and then i'll create a function inside it that will return the retrofit instance url i already have it for you so here it is i'm going to make call to this url and this function will return us retrofit instance we'll import it and here i am adding the json converter factory and when we make the api call using retro so we need an interface so i'm creating another interface with the retro service interface and inside that will define another function with the http type that is the get here i will define a function and this function will return a list of some data types so we'll create a model class before that let's add a complete url so v2 will be passed as the parameter let's create a model class or we can say a data class and you can give any name i am giving name as a country model inside this data class will having the same parameters so this is the json when we'll be making the api calls we'll get this and it have some parameters so we'll use the same name second parameter is going to be let's get it from this so this is the flag url so it is flag next parameter will be display on recyclerview the alpha two code that is a two digit code for each country then the capital and then the reason in which these and it belongs and we'll use the same country model and pass it to our interface so this interface is also ready now it's time to create another package that is for our view model inside this package will be keeping the view model and since we have our activity name is main activities i am using the same name for our view model so that is main activity view model now i'm defining a live data mutable live data on that we'll be setting up some observers inside our main activity inside this init blog will initialize this mutable live data and now i'm creating a function which is going to just return this light data and now i'm creating another function which is responsible for making the api call so first of all we will try the retrofit instance which we already created so this function inside the retron stance class will return us the retro instance now with the help of return stance we'll get the instance of our retro service interface and this retro service interface instance will return us call and this is the type of list country model and then we'll simply encue it and then we'll set up the callback type it is going to be the list and the country model and will simply overwrite true function that is on failure and on response so this function is called when our depends on our api response so if it is fail then we get the call back inside our own failure and if it is on failure then we'll simply return null and if it is success then we'll simply return the data so our view model is ready now it's time to implement our activity so first of all let's add a recycler view inside our layout file now inside our main activity we'll be simply initializing our view model and setting up the linear layout manager and adapter so linear layout manager i am adding just to shorten the linearly if you want you can set it up horizontally or it's totally up to your requirement or you can set the grid layout manager now i'm creating a adapter package and the class for our adapter because as we know with the recyclerview we need adapter this adapter will simply setting up the row on our recycler list now we'll simply override three functions so my view folder is not yet created so let's create that myviewholder class now we need our data the way we need to get it from the activity so it's going to be simple list and type of country model and we need to have a function where we can set up this data or we can update this data from outside and if list is null then return 0 and return the size of our list now we'll implement oncreateviewholder so here we'll be inflating our layout file so first of all when we are inflating we need a layout file so let's it's time to create a layout file for our recyclerview row and in this layout file i'm simply adding one image view and two three text views just to display the country name country reason country capital and this image is just to display the flag of the country so this texture is for the capital and another texture is for reason so layout file is ready we'll use the same layout file name inside our adapter and then we'll pass this view to our my viewport and inside our myviewholder class we'll simply inflate our layouts that is the first image view that is for the flag we'll import it second text view is just for the country name third textview is for the capital and fourth one is just for reason and we'll set the data so we need the country model object so now we'll set first let's set the name and along with this we'll set the alpha to code here as well inside the brackets and this is the capital i'm setting up and now we need to use the glide to vector u library so here glide doesn't work with the svg files so that's the reason i'm using this glide to back to you as all the url in the api uh for the flag having the svg urls so for that we need the instance so for activity so i'll take it in the constructor of this adapter and then we'll pass it to the view holder class and then we'll pass the url uri dot path then the url and the view where we want to set that image and now we'll implement our onbindviewholder function and will pass the list data or the country model and the activity so our adapter is ready now let's go back to our main activity and set it to the recycler view now we need to create another function where we'll be initializing our view model and we'll be setting up the live data observer so first of all let's get the instance of our viewmodel and then we'll get the view model dot get live data observer and we'll set up the observer on it will check null here because in case of failure we are sending null so if it is not null then we'll set it to the adapter and we'll call notify data set change in case it is null then we'll simply display our toast and we'll show error message to the user and then we'll call make api call function and we are still missing one step like we need internet permission in our manifest file so let's add those permission before we run our application so let's double check we have recyclerview adapter inside our adapter we have everything on place this is layout file this is the interface this is retrofit this is model let's run our application and see how it look like there you go so you can see now the svg file is downloaded all the flags are downloaded you can see the name capital reason everything on place so you can see now as i mentioned the urls are having the svg so for that only i use cloud glide to vector you so guys that's all for today's video thank you so much for watching if you have any question please ask me in the comment section thank you
Info
Channel: LearningWorldz
Views: 2,106
Rating: 4.9166665 out of 5
Keywords:
Id: zgiUnfaHP7A
Channel Id: undefined
Length: 20min 5sec (1205 seconds)
Published: Sat Jul 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.