#10. Load Country data in Recyclerview from API (json) | Covid-19 Tracker App using Android Studio

Just another WordPress site

#10. Load Country data in Recyclerview from API (json) | Covid-19 Tracker App using Android Studio

hey guys this is Shubham and welcome to another video lecture of this coed 19 tracker app tutorial series in the last video we we have seen how we can face the data from the API for the district of every state and we’ll load in a ListView so that it will show us like a list and we have implemented this search feature in this list and we have also added the click event on each item so that so that if you click on any item for that district you will get in another activity with the details of that district and you will have a title on the title bar of it that district name so in this video we will be going to implement to fetch the data for country and we will load the data in a new activity just like a list it will be shown like a list of countries and we will set the onclicklistener on each item so that if you click on any country then it will take you to the next page in which you will have all the data for that particular country which you have selected so let’s get started with this video so let’s first start the world activity world data activity in which we will implement our on click method inside this first we will disable this toast line now we will add our code so I have already added the codes in our in my repository so I will just copy from there and I paste it in my project so what this line will do is this line will basically create a new intent so I have written the start activity and inside this I have passed an instance of the intent and in inside this intent I am going from old data activity to country-wise data activity to our class so I will just go to this new activity so currently it is not available in our project so I will quickly create a new activity with this name ok so I have created our new activity in which we will show the list of countries with their respective total cases data now we will add a layout to show in the list for the country so we need a different layout because we need to add a flag image in the list for every for every country so we will add a new layout so we’ll go to this resources folder and in we will create a layout and we call it as layout you country-wise okay our layout is created now I will go to my posit or and we’ll copy the codes for this layout and we’ll paste it so I have pasted my code inside my layout custom layout so here we have a guard view an inside card view we have a text view which will show the rank means the number for every country after that we have another card view inside which we have placed an image view so this image view will basically show the country flag flag image so we will fetch it from the API and then we will have a text view which will show the country name and at the last the text view which will show us the total cases data for that country so it it is our custom layout which we will populate in our page using the adapter so next thing we will create is the model country wise model so that we can fit we can get the data and use it in our adapter so let’s create it first okay we will copy our code from repository so from the API from the country appear we are getting the country name the confirm cases the new confirm cases the active cases the deceased cases the new deceased cases the recovered cases the tests total tests and at last the flag the flag which we will receive is basically a URL link for that image the flag image so we will quickly create constructor for all the with all these

values and after that we will create get methods for all the values I model is ready now I will create our adapter to load this layout to our page we’ll call it country wise adapter and inside it will extend the recyclerview dot adapter class and inside this we will pass a last name my view older so currently it is not created so I will create it over over this and you will get an option to create a class with this name and this class will extend recyclerview dot vivre older class where you will refine all your views of your custom layout and create the matching constructor for this but for this for this class now over over this and you implement all the methods for your recyclerview class now I will just go to my github repo and I will quickly copy all the necessary things if you are watching this video for the first time before watching any videos any previous video so I recommend that first a you will you watch the previous videos then only you will able to understand this because I have explained everything in my previous videos that’s why I’m just directly copying and pasting the codes in our project so if you have any doubt you can check out my previous videos you will find your answer and if after even seeing my previous videos will not able to understand then you’ll please let me know in the comment section I will try to answer every questions you inside online few viewholder will bind our data ok we need to first define our revels in the class which my viewer class you so we are using light to load our URL image into our views so I will tell you once I import all these views okay so glide is a library which loads the data into your image view so I’m using the context here and with glide I am loading this your country flag URL into our image view and this disk cache strategy it is why I am using it I’m using it because if you if you are fetching a URL which has a high quality image so every time you load the data it will it will load a few seconds later so for that time you you will have nothing in your image view that’s why if for the first time if you load if anyone loads the data so it will take time and after that it will be saved in your cache so again if you fetch that page then it will not take the image from the URL it will take the image from the cache memory that’s why I added this disk cache strat strategy here and inside the gate item count we will return the length of the array list so adapter is completed now I will design my country wise data activities

XML file okay I will copy from my code so you will it will be as same like we did for this state and for the district’s country wise will will also have a search search box and header with a card view and we’ll have a recycler view you as you can see I pasted the code here and I I got the same layout as I as I have for state and for district so nothing changed here just I have changed the IDs so now we will implement our code in the country wise data activity from to load the data from API so we will first define all our variables you then we will set our title bar and we will set an icon now we will add an init method inside our on create method to initialize all our views and variables and we will add an override method on option selected item selected so that whenever you click the back button your activity will be closed current activity will be closed so you will get get back to the previous page now I will write the code to fetch the country data and we’ll load into the recyclerview so I will quickly copy the codes from here you let me import all the fields first you okay we have imported all the classes now this is our API to fetch the data for all the countries we will open this quickly and we’ll see what is in this API so this is our JSON data it is very unreadable but I will explain you what it’s inside this so this is the country this first object is country we are fighting through the country of first country is written as Afghanistan and the country info is another key inside which we will have an object in which we are having the flag URL image URL as you can see so we will face the flag URL using this country info object by fetching this object so and the next next values we will fetch by cases will be the confirm cases today cases debts two digits recovered today recovered active and these fields we will use so let’s go to into the project as you can see we have created a scenario request and inside this we have passed all the required parameters and in the fourth parameter we will listen to the response and we will write our code inside this on response method so we will first clear our ArrayList which we have created to load in the recyclerview because if we don’t clear this this array list then it will load duplicate it will make it duplicate means if you if it add ten items then again if this this method runs then it will again add the ten items so it will be 20 so it will be duplicating so that’s why every time this method is called we are clearing the ArrayList so we are lower we are running a loop from 0 to the

length of the string the response which we are getting so here this 0 index is the first country which is the Afghanistan here I am creating a JSON object instance and we are saving the object inside this this instance so the first instance will be saved as this from here until here so this is the first this is the first object and again the index one will have this second object second object will have country Albania so just like that we will fetch the data so once we get the instance object of of from the JSON data then we will assign the values by accessing the key so for the country key we’ll have the country name for the cases key we will have the cases count so like for everything we will fetch the data from the API and we will store the data into the strings which we have defined at the top but for the but for fetching the flag we cannot directly fetch through the key first we will have to initialize an object okay because it is under this under this country info we’ll have an object not a value so we will have to initialize an object and assign this of this object in to that object and then we will fetch this flag key value that’s why I I did it here so that’s what I did here I have just created an object and we have saved the object inside this country info to this object and then we will fetch the data from the flat key and saved it inside this strain so that’s how I have created this I fished the data and saved into the strings now I will create an object of country-wise model and we will store this data I will pass this data into this constructor then we will add this model into our ArrayList it’s very simple and then we are creative ear and then we are initializing a handler post handler so so that we can make a delay of one second or any second you want to show the dialogue dialog box so we will just notify the changes to the adapter in this so that if any changes is made so that it will be updated in the recycler view and then we will dismiss the dialogue that’s all in this code so let’s see it by running here if it is working or not I’ve run the app let’s wait for some time so our app is loaded now we will click on the walls data and then we’ll click on the country data so it should show all the data so as you can see we are having all the data here with the flag okay we have only India everywhere there is some issue some you show some issue let’s see if I steer country are I’m getting the STR country you let’s check the adaptation okay I’m not I have not initialized the name here so let’s quickly copy a name from the adapter so here it is the coal – just highlighting the text which you have searched so this thing I have already explained in previous videos so if you

want to know you you can check out my previous videos so that you will get all the informations okay now let’s again run this app and we’ll see the results you okay our app is loaded now click on verse data and we will go to the country data and here we’ll have all the country’s name with the fire with the flag and the total cases so that how we have implemented the list and the search feature for we don’t have implemented yet so we will quickly add the code for the filter so we will first add the text for church for the text inside our on create method okay we have a drip detects water now we will create a filter method you so it will call a filter list from our adapter so we will add that in the adapter also here this swing this method is we will add into our adapter okay now the error is gone and we will also add the fetch fetching feature so we will copy the fetching feature so in this fetching feature we will just add a refresh again so when you would swipe this swipe down it will again call this method which country data and we’ll close the animation so just like that so we have completed this part now we will check again if everything works fine or not okay our activity is loaded you click on world’s data you country data let’s check the search feature if it’s working or not okay it is working fine and if now we will implement the click listener if we click on a particular country then it will open a new activity and the data will be shown – for that particular country in that activity so we we already faced all the data is for for every country in this page now we will just forward that into the next activity by using the put extra method of the intent class so let’s see how we can implement that actually we have already implemented this this thing in previous videos for the state state data state wise data in district wise data so we will just copy the code from the from my git repository and will briefly explain everything so in the adapter class we will add our listener so here I have already added this linear layout we will set the onclicklistener on this linear layout so we will click quickly copy the codes from the repository and we will add our on click listener inside the on Pineview holder class on buying a few holder method sorry so we are basically adding an intent and we are going to each country rate activity currently this activity is not

available so I will quickly add this activity into our project name should be the same you okay well activities created now we will add some constant so that we can use as a key to pass the data into our new activity so we’ll quickly copy those from here you constants so we have added the constants of we need to import this in this adapter you if we have added all the keys now we will design our layout for the each country data activity so quickly add the course from my github repo for each country data activity so here we don’t have the swipe refresh layout because the data we are getting in this activities from the previous and not we are not fetching an in data from server or any penny from anywhere we already have the data so we have not added this swipe refresh so we have added our layout now actually we don’t have here the new recipient recovered and the new active cases so we will keep it as an a not available for these two fields only the new cases so let’s go to the each country data activity and we will implement this here so first we will define all the views for the variables at the top in the glass then we will first fetch the data which we are getting from the previous activity and we will add the gate indent and we will also add the on option item selected so that we will we can close the app when whenever you you click on the back icon on the toolbar now I am accessing the values which we have passed using these keys you you okay we have added all these and we are we have stored the names here now we will just load the data you before loading we will customize our toolbar so we will set the name of country in the toolbar title and we have enabled the back icon on the toolbar and

then we will initialize all the views you which we have defined at the top of top inside the glass okay we have define every fails now we will load the data into this Fields you I have used the post handler to make a delay of one second so that it will it will look like it it’s fetching the data like right to Saudi to show the progress dialogue that’s why I’ve used this one-second delay and I’ve set the new active as na and Neil covered as any because we don’t get a we are not getting the data from the API for these two fields for set is at as any now we will call this method inside our on create method so that’s all get intent is not called you you that is written get intent should be outside you of this class sorry and this will also be outside you you you you you you okay well this activity is completed so let’s check by running it you so our app is loaded let’s wait for a few seconds okay it is open now we will click on the world’s data in the country better and we have the list here and if we click on any country it will open a page and show me the detail of that country right so that’s how it is working guys now if we want to show the data in descending order of total cases like if the for the total cases for the country which is the max will show on the top miss ranking ranking wise so we will implement that now we’ll copy the code from the ape from our from our repository so we will add the code or in our country wise data activity where we have added the adapter we have added the ArrayList so we will short the ArrayList here after creating the ArrayList we will

short the error list okay now let’s take the app again you the app is loaded now let’s go to the world’s data and we’ll go to this country data and we will see this is now in descending order so as you can see the at first rang USA is and the second ranked Brazil third ranked India fourth Rossio by Peru it is showing like in the descending order so we have done almost everything in in this app so in the next video we will set up a splash screen whenever you open the app so first it will show you the splash screen and we will also add the about Us page here when you click on this icon you will get a page which includes all the information of your app and you so that’s all from for this video if you like this video then please hit the like button and subscribe to my YouTube channel and don’t forget to press the bell icon to get the updates of my latest videos and if you are facing any kind of issue with the codes or you have any doubt so feel free to add your comments in the comment section below and you can connect with me on the social media platforms also I have already given the links in the description below so you can check that and if you have an issue you can directly ping me also and do for the codes you can check out my github repository I have also given the link in the description below so just go to that and do experiments on the codes try to try to see changings changing in your way the UI or the codes or you can you can do more you can further you can further minimize the codes like you can make it more less complex so let me know if you have done that so that’s it see you in the another video guys bye you