Recycler View Android Example with adapter, list sorting, menus, global variable and glide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there welcome to a tutorial with android studio in this application we're going to create a list of presidents and it'll be scrollable and it'll be editable and so we're going to work with the recyclerview [Music] so if you're interested in working with the recyclerview follow along here what this app is going to do is have a scrollable list of cards now what do i mean by a card so you can see that in this app each item has three components so we have a photo a name and a date so an integer string and another picture and so that is what i would consider a card and so this will show you how to put that together and scroll through it using what's called a recycler view now a recyclerview has different types you can have a compound version like a card versus a simple layout which is just a single line of text now the simple line of text is really quick and easy but the more compound version of course is more complex that's going to require us to use something called an adapter an adapter is going to be how to make a class such as president fit together with the layout the activity or the screen and so you can see that the adapter is to say i'm going to take the properties like name year and photo and merge them together with their proper components on the screen and so that's called a class to view adapter now here's what the application's going to look like when we're done as you can see i can scroll through the list and see a bunch of presidents i can add one if i want and i can type in his name date and then i paste in a url from the internet and that would create a new person now i can also sort this so we're going to get into sorting where we can alphabetize it if we want to put abraham first and then the rest of them or if we want to sort them by year we can get back to the original george washington being first and so when we click on somebody we can also edit that person so thomas jefferson i could change his name if i wanted to or change to a different photo so that's what we're going for here it's a lot of different things to make this recycler app work like a pretty good demonstration of a lot of things that are used in many applications now when you work with an adapter you're going to use it with lots of things in not just lists that you can scroll through but for instance a spinner or a drop down list you can have to have a an adapter to make that work so anytime you see a list of choices or things that you scroll through you're going to need this another concept that you're going to see in this tutorial is the concept of a global variable in an application so obviously we have a list of presidents that we're working with and you can see on the left here my list it's just a list of objects now where am i going to save that well what i want to do is have a central location in my application called a global variable and so that's kind of a tricky thing that not most people need to do but you're going to see it in this case what this allows is the activity one to show the entire list of presidents and then activity two is to edit or show one president and both of them are going to share a common list of data so we're not going to save this list in either one of these activities we're going to make it a centrally globalized place now some people would argue that if you're going to use a global a globalized storage location then you should probably set up a database such as sql lite and so then you would be able to share data with that database directly to the activity and activity2 could also update or read from that database so if you want to know how to use sql lite as a database tool i have a video for that you can see the link in the up to i'm on top and in the description but for right now we're going to use what's called a global variable because it's just a quick way to do it now the application is also going to be able to sort and so we're going to implement a a menu where you can click on the top right corner of the screen and choose one of the options then we're going to have to be able to do some java programming to sort a list that's not just android that's about sorting in general and then updating the list so that way it's in a different order than it was before each item is going to have a click listener so if i click on abraham lincoln's face i will bring up the items to say i want to edit abraham lincoln's details so that's what the application is ahead of us i'm going to split it up into pieces so that way if you're interested in just one of these concepts you can see those or if you follow it from beginning to end sequentially you'll have the app as you see it right here so let's get started with android studio building our recyclerview app my name is chad sluder and i'm a professor of software development computer science so if you're interested in becoming a software developer and you'd like to kind of watch what's going on in my classroom please subscribe here and make sure you click the bell so you're updated whenever there is a new lesson that we're doing in class so welcome aboard and let's get started with some android development hey welcome to the first part of our development process of our app we'll be we're building a recyclerview app where we're going to show a list of presidents and we're going to be able to edit them and sort them [Music] so in this video we are going to work on the layouts and the navigation so we're going to create the screen that shows the list view and then we're going to create a second screen that shows an edit view where we can update one of these records and we'll be able to navigate between these two screens so that's the next part so let's get started right into android studio you can see that the version of android studio that i'm at is 4.0.1 and i have a emulator already running so let's start a new project and build these views so i'm going to start a new project using an empty activity and choose next and let's call this thing an application let's call it president list or something so i'm going to name mine as president list recycle view and you can see the package name is my url it has a location on my hard drive and i can choose either language i'm going to choose java for this one and i'm going to choose lollipop which is api 21 which runs on most devices i'm not going to check the legacy android support libraries and choose finish so if this is your first time into the application i recommend maybe a different tutorial i've got a beginner's guide to android studio this is going to be much more complicated than a first timer at android studio should try but let's get started with the res folder and the layouts and let's take a look at activity main this is where we're going to design our layout so let's start by knocking out hello world so i'll select him and press delete and i am going to put in a button at the top which is going to be for add one and so let's anchor him in so i'm going to drag to the top i'm going to right click and choose center horizontally and then let's give it a button id now in my case i always use btn underscore and then the name of my button so you might have a different kind of a naming schema but this is what i use here and let's give it some margin from the top so let's go for the text down here for the text i'm going to say add one all right so our button is in place now the only other thing that this part needs is a recycler view so if you choose recycler view or if you go to legacy and choose list view you'll get what looks like the same results but obviously legacy means old and we don't use that anymore so they're preferring us to use recyclerview so i'm going to drag a recyclerview out and put it on the screen now you've got a message that says we need to add a project dependency so we'll do this we'll click ok but then we'll figure out where that goes so i click ok and you see the arrow also here says we want to add a project dependency so what's a dependency and what did it do to our project so let's go look here and let's go down into the gradle area and choose the second one called gradle module and let's see what's going on in here so we scroll down and we have a section called dependencies now these are plugins that enhance the function of your application so for example let's say you wanted to have a graphing tool where you just gave it some numbers and you get beautiful pie charts or something like that you probably don't want to have to program all that you just take somebody else's module and plug it into yours well what we've done here is we've added a recyclerview right here on this line so we're coming from android x recyclerview and we are at version 110 so if you're from the future you probably have a larger version number than i do so pick the most recent version that they have and go with that now let's go and see what it looks like so our layout now shows that we have a item with a bunch of things in it so let's zoom in on it and see what that might be so it says zero one through nine all right what do we need to do well we got some problems here it says we need to anchor this around so i'm going to select here and drag it to the bottom of the button it says how do you want to anchor these so i want the top of my list to be at the bottom of button one let's choose that and let's go and take a look at the margins let's give it a little bit more space eight pixels okay so the next thing down is we see the layout width and height are actual numbers so what i want to do instead of add these is numbers i'm going to right click on this thing and do a center horizontally and then we have constraints set to the sides then for the width i can say let's match the constraint and for the height let's also match the constraint and let's see i want to constrain this to the bottom so i'm going to click the plus sign and let's see let's set that to 0 as well all right so it looks to me like i'm done with the first layout we've got add one at the top and then we have a list so we need one more thing we need an id for this so i'm going to use lv for my prefix and i'm going to call this thing president list we're done now we're going to make the second layout so let's go to file choose new all the way down into layouts activities is what i meant to say and another empty activity for this activity i'm going to call this thing add or edit one so that means that we're going to either add a new president or update an existing one and let's click finish all right so let's get to the layout part here so i'm going to choose the xml file and we are in a constrained layout which is fine we are going to add a bunch of text views so i'm going to put five text views out here these serve as pretty much labels they're not actual places where we enter text that'll come in a minute now these are just labels so i'm going to give them actual text and so the first one is going to be the id number of the president the second one is his name the third is the date when he was elected and then the fourth one is going to be the url of his image and so we'll just use those text items as their labels so i think we should have a little bit bigger text for each one of these so i'm going to set them to be 30sp for each one of them next it's time to set their constraints so i'm going to start with the top one here and constrain him to the top edge of the screen and to the left edge of the screen then let's go look at the margins and let's add maybe eight pixels for each the top and the left let's do the same thing for name now i will attach him to the bottom and to the left let's follow through with the rest of these controls so we will set the top to match the bottom of the previous control and we will set the margin of space between each of them as 8 pixels and so when you're done you will have a nicely aligned list of labels id name date and image url now we still have this little orphan id over here the text view let's drag him to be at the top of the id and let's do the left to match the end of the id let's also set some space between those now in this one it's important that we actually give it an id so i'm going to put in here tv for text view and i'm going to call it president id number for the president id number i'm going to set his text to 0 and the font to be 30 again now for the rest of the form we're going to have to use a different kind of text view we're going to use plain text which is used for data entry so we'll drag some plain text out here let's put three of them on the screen so now the goal is to line up all of these edit texts to their labels so i will constrain each of them to the top of the button that was in front of them so that way the top of the button and the top of the label both line up now i'm also going to give them a space of 8 pixels so there's a little bit of margin between them and so once we're all done here we will have ourselves a nicely aligned form now let's give these things ids and as well change their hint text so let's start with the first one here and so this thing is an id so we'll put et for edit text and we're going to call this thing president name now down at the bottom here we have text i'm going to erase that and instead up in the top where it says hint i'm going to put in the name okay so you can see that name shows up but now instead of being in dark text it's light text let's go to the date now and let's give this thing a name as well so the i or the edit text is the uh date of election we'll call it et date election now we'll do the same down below where we're going to erase the text for the name and change it to the hint change it to a date the last one we're also going to name as et underscore but this we're going to call picture url okay so we got all the edit items in now i'm going to put some buttons in so let's go either to the buttons area or common and find the button item let's drag one and two in so let's give these things ids so the first one i'm going to call this thing button ok and the next one i'm going to call button cancel so i've got their ids set and now i have text let's align them so that they look nice so for the alignment i'm going to align this first button to the bottom of the previous item and let's give it some space here so let's give it maybe 16 pixels of space for the cancel let's line him up to be the top of the previous button so that they're both at the same height now i want to center them so this is a little tricky so the first item i want to do is i want to center or i want to drag him to the left the second item i want to anchor him to the right now i want to anchor them to each other so that they float so let's go to the ok button and i will try to anchor them like so so he is anchored to the start of the cancel button now i can't go the other way unfortunately if i try to anchor him to the previous it never seems to work but don't worry we can go and change this in the code so i'm going to up to the top right here choosing split so i can see the xml code that i'm working on right here so what do we got here the cancel button that's what i'm looking for so edit text come down to cancel button there we go and the constraints so i have two constraints set up i'm going to insert some space and put a new one in so i'm going to type in the word constraint and i want to do the constraint where the start of the button let's see constraint i'm going to just type it in straight constraint we want to do the start is going to the end of a previous button so the end of the id number of okay okay so we're manually putting them together and as soon as we put that in there you can see that the buttons now are relatively spread across the screen okay that's the alignments that's our layouts next video we're going to put some navigation in so we can get from one screen to the next hey what's up we're in the middle of a project here we're making a recyclerview app you can see that it's going to be a list of presidents and a button that says add one in this video we're going to work on the navigation previously we created the layouts for each of these and so now when we click one button we will go to the next screen and vice versa so we're going to do intents and navigation between screens [Music] so in the previous video i gave you a couple of tips on how to build these interfaces so activity one has a button and a list activity two has got a bunch of items where we can enter in a new record so we want to be able to switch between these so let's go to main activity and start there so just double checking that when i look at my items i have an id called button add one and that's going to be the first thing that will program so let's switch into the main activity java program now so if you're very familiar with intents and navigation of course skip ahead about 10 minutes to the end of the video and we'll go on to the next part but if you want to see how this works let's go ahead and code first thing i need to do is set up some references to these buttons so i'm going to have a button and i'm going to call it btn and let's call it uh what was it i think was add one so that's the name of the button that i'm looking for and you can see that it says i don't recognize this we have to import the class so you can either choose here or press alt and enter and now in the import section you can see that we are now using the widget button on our program so i'll just hide that it doesn't need to be there now we get into on create this is where you have to create the reference and talk about which item in the layout that it matches with so we'll say here we're going to define the button add one it's going to be equal to find view by id r for resources and we're looking for id number and we should see btn somewhere so we have three buttons in our program and the one that i'm looking for is the first called add one so i'll just press enter and we have now a reference now we need to add a click listener to that guy so one way to do that is type in btn add1 dot set on click listener and then inside there we make a new on click listener and you can see i'm using type ahead all the time and it provides a lot of the code for me so what happens when i click this thing well i want to create an intent so i'm going to call this thing intent with a lowercase i press alt enter to import the intent now to do an intent you need to create a new version of an intent we need two parameters in here the first one is the context of the activity we're working on the second parameter is the destination that we're headed to so how do you get the context of this thing so i'm just going to type in the word this now this is pretty strange what is context and where do you get it so i have another entire video that describes what context is so check the link above or the description below and follow on what context is this is a reference to the activity so i could put in this or if you prefer i could put in main activity dot this and get the same thing so i'm going to leave it as mainactivity.this just so that way it's it's obvious of which activity this is associated with now where's this going to go so the destination is the other java activity which is add edit one so when i type in add it suggests here this guy now i need to put in a dot and the word class afterwards so it tells me which java class it's supposed to enact or invoke now what do we do we want to start the activity so i type in the word start activity and inside there we type in the name of the intent which in this case is lowercase intent so we define the intent here and now we use it here so this is a basic navigation we're not sending any values between these guys so this is the simplest one you'll get i'm going to save this and test it out let's see what it looks like so i'm going to run the app and then bring up my emulator so my emulator is already set up and running if you haven't gotten one yet you probably have to set that up first of course or plug in your phone and you can test it on your own device but it's easier to show it here on the screen because i'm screen casting hey great news it says my gradle invocation is finished it should be loading my app now and sure enough here comes the screen and i have the recycler view let's see what add one does i click here and it does bring me up to the next screen now if i click these guys obviously nothing happens so that's the next part of our intents now if you want let's just see if you can do these yourself so go to the next activity and see if you can create an intent for each of these buttons that will take you back to the main activity you can use this as a model they look exactly the same except you just switch different names around but you should be able to do it so pause the video if you want to try it on your own otherwise stick with me and i'll code these on okay so let's code the other guys so we have this intent i'm going to copy it because i know that the next item is going to be very similar and i'll just want to avoid some typing so i'm going to copy this let's go to add we need to create references to these buttons so let's go to the top line here and create a new button and i'm going to call it button ok and another one called button cancel and let's go ahead and import buttons inside oncreate we're going to make an actual assignment to these values so we're going to say ok is going to equal to be find view by id go to the resource folder try id and we should see button ok come up as one of our choices so i'll switch ok same thing happens for cancel so i'm going to type in btn and select cancel and choose find by id let's see if i can find that r dot id button cancel so now you can see it's important that you name your button ids something that's sensible so whenever i see btn i know that i'm working with a button all right now it's time to add some click listeners here so let's type in button ok for the first one set a new click listener do an on new click listener and now since i've pasted or copied that code earlier i can just paste it in here and i've got myself an ok button that should reverse back to the original screen all right so now it's time to put in the cancel button so let's type in btn cancel set on click listener is what i'm looking for i'm going to create a new on click listener and paste in the code so there could be a better way maybe if you start using identical code in two locations you might think there's something wrong here there's other ways to make click listeners but this will get us started let's test it and see if this thing actually works with both buttons so i'm going to refresh my app and bring up the emulator hey all right the the app didn't crash when i ran it so that means i must have typed all of these things with correct names let's try add one and now i can push the cancel button and come right back hooray it's working and the same for the ok button i'm going to cross my fingers and it also works great so we've got all the navigation we need for this simple app we can go from one screen to the next we're going to make a list and we'll have some click listeners in the list but i can't get to those right yet because we haven't coded the list we're going to now go on and create the class for the president you know the actual properties and create a list of presidents so that's coming right up hey there we're in the middle of a tutorial here creating a recyclerview app so you can see on the screen i'm going to make a list of presidents we can scroll through them click one and then update we can edit one of them or add new ones so in this video we're going to actually create the class called president and then put it into a list [Music] so the question about where to put this list is going to come up right now so we're going to have a list of items that wants to be able to be accessed from any screen on our app so we've got two screens and we're eventually going to put it into a globally accessible location and that's coming up in maybe a future video but for right now we're just going to create the list inside of our first activity and so for those of you that are just sticking around for part of this tutorial i'll keep it as simple as possible and not not do this global variable thing yet but that will come eventually so we're going to put the list of presidents inside activity main so i'm switching back into my code editor and since we're going to main activity let's open that up what kind of an item do we need to create well we need a class called a president and then we can make a list of those okay so we want to create a new class let's go to the folder called java and you can see we have three different locations i'm going to put it inside this folder where the other classes are so we'll right click and choose java class and the name let's call this thing president and what are we going to have in our president well let's give it some properties so i've decided in my app that i'm going to have four different properties i'm going to give each president a unique id number which makes things easier to manage in the lists then i'm going to give him a string for president's name the integer is going to be the date of his election and then the string is going to be his image url now you can add more properties if you want to make this more detailed all right so we want this president to have some actions so we're going to need a constructor a tostring method and some getters and setters so let's do all those things so first of all i like doing all of this automatically if i can so i'm going to select all of these guys and let's do a right click and choose generate and what can we generate here let's start with a constructor and so let's see i'll select everybody here and click ok so here's the constructor so it has four parameters you can see as the inputs and each of those are assigned the properties according to their name so all that code's done very nice let's go to the bottom here and let's now create a bunch of getters and setters do the same thing i'm going to right click choose generate and then choose getters and setters this time now i'm going to select all of these with a shift key so i have all four and click ok and now i've got get id set id get name set name the whole thing so that's pretty standard practice when you're creating a bunch of properties now we're going to probably need a two string methods so i might also put that in now let's do a right click generate and let's do a two string method and it'll take all of those guys and it will concatenate all the strings together so that way when we're logging out information in the in the console log we'll be able to test to see which president's been added or deleted okay so we've got that we're going to also do some sorting but we'll have to come back and figure out how to compare objects at a different time for right now this is good enough so i'm going to save this and i might also close it i don't need to look at the president class anymore now i just need to create a list so the main activity is going to be where i put the list so let's start here with the word list and type of president and let's call this thing what president list how's that and we're going to make this a new arraylist of type president there we go so we've got ourselves a new list looks like we need to press alt and enter or return to make that list happen now it's an empty list let's add some data to it so maybe down here in the first line of oncreate i'm going to create a new function called fill president list and uh that's just going to be a method that doesn't exist yet but the light bulb says i will create this for you if you want and so i will select that option and down at the bottom it says fill president list now for each item in the president list i'm going to create a new president so let's create president p0 and we'll use the constructor to create a new president so i'm going to use this as my model and i'm going to copy and paste it like about 10 times so i'm going to press ctrl c ctrl v and until we get a whole bunch of presidents okay so i'm going to rename them all from p0 to p9 so that gives us 10 presidents so let's fill in the first guy his uh id is zero and our first president that we're gonna put today is surprise is george washington and you knew this but he was elected in 1788 correct now where are we going to get an image url for george washington so we can just use wikipedia i like wikipedia for a lot of things so i'm going to go to my trusty browser and look for wikipedia list of presidents and sure enough the first article is a list of presidents and if i'm lucky i will see photos here they are look at that so we have ourselves george washington now i want a photo now the easiest way to get the photo is just to right click and choose image address not the image itself but the url so i'm going to choose copy image address and then i'm going to switch back into my code here and then i'm going to paste so let's see control v and wow that is a portrait of george washington so it looks like that'll work now i'm going to go through the rest of these and uh if you want to do them as you watch me you can otherwise just pause the video and fill in the rest of the presidents that you like okay so for each president now i'm going to go and copy his url so his image url and i'm going to note the year of his date so each item that we create is going to have a different id number so 0 1 2 3 4 5 6 7 8 9 and then for each president we need a string we need to have an integer and then another string so all of this still goes together pick out the presidents that you like the best or whatever object that you're using here and you will have a new president list so when we're done we should have p0 through p9 for our presidents so next i want to make this list of guys into one one list so we're going to do president list dot add all at all and inside there we're going to create a new array of these presidents and convert it to a list so it's kind of strange go arrays dot as list and then inside there we create a new a new array so the reason i'm doing this is because i can put it all in one line you could also just do this 10 times using the word add you could add p0 you could add p1 you could add p2 but that would take a lot more code so anyway i'm just trying to make it short so when we're done we should have a list of presidents okay so let's go up to the top and see if this actually works so i'm going to now put in a log so i'm going to put in log d you see that i'm going to press enter and it says we have a new message that's going into our log well also tag says we need to be defined so i'm going to create a tag and the tag up here at the top is going to be called our president's app and now in the log i'm going to say uncreate and i'm going to add a president's lists dot 2 string so this is just for testing purposes we're going to log out all of the presidents after we fill them so let's check it to see if it runs i'm going to run the app and see if there's anything in our log okay so the app is a coupling is it running yet let's get it started so we got the app up and running and let's check the log out so i'm going to open this up choose logcat and we should see some new messages near the bottom here and sure enough we've got an uncreate message here so let's see if that looks like it so i scroll to the right and i can see that each president has been added so it looks like the list has items in it that's pretty good one more check to make sure that it's working is i'm going to put a toast on the screen so i'm going to type in the word toast i'm going to select create new toasts and i'm going to tell us how long the list is so i'm going to put in the president count and i'm going to say the president list dot size and that should bring up a message on the screen to tell me how many presidents are in my list and let's see i'll run the app and it'll only show up for a second but down here at the bottom it says president count equals 10. so we've got ourselves a list in the next video we're going to actually put that list together into something that we can see on the screen so that's coming right up hey there welcome to this app on recyclerview in this video we're going to take a list of presidents that are in a java class and present them as a recycler view [Music] so i think this is like the fifth part of our video but if you are just joining us we are creating layouts and then we are going to be able to edit those so we're going to now do the actual code of a list adapter so here's the app as we left it before as the you can see on the main activity we have a list of all the presidents that we want to put into our program and we've initiated it but we have not actually displayed them on the screen so to make this happen we have to create a layout for each line item so for instance here in this case george washington you can see he's got a string of text he's got a number for his date and a photo and they're arranged in a specific order so to make that happen we create a layout or another view that will be for each line and there's also a horizontal divider here that you may not have noticed so since we need to create a new layout let's go and choose that in our layout section so i'm going to right click choose new and we're going to do a layout resource file so i've got to name this thing so i'm gonna type in one and you can see it says it doesn't like capital letters so let's try this again one underscore line president and so that'll tell me that what one president's layout will look like so it's gonna be up in the top center or top of the corner here we don't have to make this a huge layout so what do we have here we have an image view so let's drag out the image view and i just use this avatar for now and for the items that are next to it we have a text view that's going to be his name and then we have his date so those are the two other items now we just have to constrain them also i promised you that we're going to have a horizontal divider between each of these i think that's in the folder called widgets let's go to the bottom here and here it is horizontal divider so let's just drag him out for now okay so now we have to put the constraints in and then we can make this actually work in an adapter view so let's start here with a picture and i'm going to constrain this to the top and to the left and let's set these as 8 pixel padding in between each of those for the text view let's do a similar idea let's drag him there let's drag this thing to the top of the image view and let's set the space there to be about eight same thing for here we're gonna do a connection and another connection so we're connecting all these guys relative to each other okay now the horizontal that has to be attached to the bottom of the photo it looks like and let's give eight pixels there okay now it's time to give them all ids so let's start with the image view and let's give that thing as an iv for his prefix and we'll call it president picture and let's see yes we have to change something so i will choose yes for the first text view this is going to be called tv underscore president name and let's give this thing a date here so let's go tv underscore date and we'll call it date of election and i think we're ready to go so i don't really care about the divider i'm not going to use him as a id number so let's save that and now we're going to go and create the adapter to make this class and each of these lines fit together so before i forget i need to go and add one more item to this layout i'm going to split the screen and look at the code and you can see i'm adding here a an id so the id for this layout we're going to call this thing one line president layout how about so we're going to need that in a few minutes so now the idea we're going to create next is an adapter that will make this layout that we've just created fit together with a class president that we've also just created so recall that the goal here is to take the class president and make it so it fits with the layout that we just created the activity so the adapter is the key piece that makes these two fit together so honestly this is a process that i look at the documentation every time i have to do it so i'm looking at the developers documentation here about creating a list with a recycler view and what we're trying to do is something that's shown here on the right where we have pictures and elements in our screen so let's go scroll through here this tells us a little bit about some of the things we need to do and i think we've already done these you need to import a dependency so in the instructions here we're going to copy and paste some code so we are back to main activity and so we are going to need some references to three things it looks like we're going to have to have a recyclerview a recyclerview adapter and a layout manager so this recyclerview adapter we're going to create in a minute but let's just copy all of these for right now i'm going to copy these and switch back to main activity and let's paste them right up here in the top with all of our class list variables so let's see if we can import these things so we got to import the recyclerview class so we have these three variables defined now let's go get them some values so they're saying that down here at the in the section here we're going to have a couple of items we're going to have the recyclerview first of all he's going to be defined as something that comes from our layout so let's go and put that in there so for my recyclerview value i'm going to give this something from the layouts find view by id is the function and i'm looking for r dot id and it looks to me like i called it lv underscore president list so recyclerview is going to have a value now we're going to look at the next items it says here has fixed size and the rest of these we're going to borrow as well so i'm going to copy and go back into my code let's go ahead and paste those so uh it recommends using this has fixed size to be true so let's leave it that now for the layout manager so we defined a layout manager at the top did we not so here it is a layout managers so we have to import the class we have to set it the recycler view gets the set layout manager to here okay so now we have a another problem here we've got something called my adapter and this has not been defined my data set has been defined i'm going to change that to was it called president list there we go so we do have a data set and then we're going to hopefully link these two together so what's missing now is the adapter part so my adapter is a class that doesn't exist okay so i'm going to change it from my adapter and i'm going to call it recycle view adapter well it does need to be created so let's go ahead and choose create the class and where are we going to put this thing so it's going to be in the destination of the regular okay so there it is we've got ourselves the recycle view adapter and it's already extending this thing called recycle view adapter now i'm going to add a few things to it first of all i'm going to give it a list of presidents so these two items that i'm going to create as class variables are going to be the list of presidents and then it's also going to be the context so uh instead of creating this constructor automatically for us i'm going to just delete it and recreate it so i'm going to right click and choose generate a new constructor and i'm going to select both properties and click ok so i got pretty much the same thing as i did before the code that i just deleted it's just that i've added this property called context and it assigns it here all right so we've got ourselves the adapter is it done not far from it we've got a lot of work to do so let's let's go look in the documentation again to see what we would add next so i'm back into the docs i scroll down here and it says a couple of things you're going to have to do so we've got ourselves my adapter it extends the recyclerview adapter and then they have here another type that we have not included so my adapter dot my view holder well my adapter isn't the right word but let's copy it anyway and we're going to change that to the class that i named so i need to put in brackets and paste and instead of my adapter i'm going to choose it in the word recycle adapter recycle view adapter okay are we getting closer my view holder it says that doesn't exist so down here it suggests to create it now if you don't get this suggestion that's okay if you click and it will just generate a new class okay so it generated our cons our new class called my view holder so the view holder is a reference to the small layout the one line layout so if we go back to one line president this item here is what we're referring to so we have three items that are interesting to us we have the let's see i'm going to switch back to design we have the president picture we have the president name and the date of the election so two text views and an image view so we're going to be defining those things here in this different items that we were just on so let's switch back into the recycler view and we're going to be defining those things here so let's go with image view first and that's going to be iv was it called president picture or something like that we don't have to get it exactly the same name but it helps to be close and the next are text views all right so we've got these three references here now i knew i do need to extend this class here so that way it it does exactly what we're supposed to on this recycler view so recyclerview viewholder is the class type that i'm working on so this will bring up a message that says you gotta create a constructor and when you do it generates this constructor here that has a parameter called itemview so item view is the reference that we're going to work on here so we're going to say that we're going to say iv president picture is going to be item viewed dot find by id r dot id and then the president's picture okay so that's going to give us the value from the layout so when we're done we're going to have the iv for the president's picture the text view for his date and their text view for his name and each of those comes from item view dot find view by id so we're getting references through this viewholder object so we're going back into the documentation let's see what we created and see if it matches anything like they have here so what we did is we just created this class here called my viewholder extends this recyclerview and then we created uh references so they had a textview in their example and then they assigned it there so hopefully my code here is a little bit more sensible to understand but this is the model that i was following so we've got these three items here and they are associated now in my view holder well are we done obviously not up here the red line says you forgot something again it's still mad at me so let's go and choose implement methods i'm supposed to do something here with methods oh my goodness i get three new methods and let's click ok and find out what those are so there is one called oncreateviewholder onbindviewholder and getitemcount well the getitemcount is the simplest one so this is the number of items that i'm going to put into my list well i have a president's list so let's just put in here presidents list dot size so the easy one is finished now what do we do with the others so let's get into this first one here that says oncreateviewholder we're going to have to create something here and return it it looks like all right so hang on with me we've got a whole bunch of stuff to do here we're going to do what's called an inflator now so we're going to create this object called a view so remember view is very generic it can be pretty much any kind of a thing that's on your screen we're going to do a layout inflator which is going to expand our layout and put it into different components from from what it says we need a pan we need a context so we need the context of our parent object so parent is a is one of the parameters passed in here then we're going to call the inflate command and inflate has three different parameters the first one is the layout that we're trying to inflate so the one line president is what i'm looking for the second is what's its root and it's it's going to be rooted to the parent and then the third is should we attach it to the root and it's false so don't worry about memorizing this this is something like i said i look up every time i have to use it because it's very specific so the next thing i want to do is create an instance of my view holder so we're going to call it holder and it's going to be equal to a new version of my view holder and we're going to insert the the view part so this is where we're linking the two together this is the adapter part that's happening so we are taking the layout and associating it now with this holder okay so that's kind of confusing but we got it and we're going now to return the holder whew that's done so when you come back to do another recyclerview copy and paste this text here and use it as a model so here you go you got something that hopefully will work now what do we do when we bind these together so when we create one of them we can do things in here such as create click listeners and assign values so now each of these items that we're going to create are the values that go with each line in our list view so the first one is the president's name so we'll do a president's name dot set text and we're going to get it from the list of data the president's list and we're going to get the item called position so where did position come from well you can see it's it's a parameter in the line just above here so the position of the list and then we'll get the name from that guy so next we're going to do the same thing for the president's election date and we're going to set the text now this is going to cause a problem you'd think that you're done here and it will crash when you run it do you see why so when we do the get date of election method it returns an integer and an integer cannot be set as set text so i'm going to avoid some problems right in here i'm just going to type in the word string dot the value of and then inside of the parentheses we're going to put in the integer that we're trying to get the integer of the date so make sure i got all those parentheses correct now the last one is going to be a little bit trickier so i've got this image view and it would be nice if we could just to set image and that would work however so what i'm going to do next is i'm going to use a library called glide and you can see as i'm typing here that glide is not part of my system it's not part of my dependencies and so i'm going to have to go import that right now and then i'll show you how it works it's a really easy way to put pictures and things so i'm searching for google again and i'm choosing glide dependency android so i'm looking for a github page how do i use glide so this is why i like it so you can see the example we're going to type in the word glide with a context we're going to load a url and tell it which image view to put it into one line of text so if you've tried to put images and bitmaps together you'll know that there's a lot of tedious work to do so glide just takes all of that into one line that's why i like glide and it's recommended by google and their documentation so it's a really good idea okay so i need to use the dependencies so let's go into these two lines here and it looks like version 411 is current so let's copy that and switch back into our gradle scripts so let's open up gradle script and at the bottom of the list just paste into two new guys and we have to synchronize them so now they will be imported into our project hopefully without any errors okay hooray down at the bottom it says the sync is finished and no problems so let's come back into here and let's see if we can make glide work so now it says we can import the class perfect so i'm going to go back to the example code and copy it so going back to the documentation i'm going to show the example this code right here looks to me like the one i want so let's copy that let's switch back now and i'm just going to paste over top so i need to change a few things but not too much so it says here we need a context well as you remember i passed a context in right here in the constructor so we can just say this dot context and away we go now the load is going to have to have a string so this is the url of the of the president so let's delete that and change this again to president list i'm going to get the president at the position and then hopefully i can do get image url perfect okay now i need to tell it where i'm going to put my image so image view is not the right thing let's go to the holder dot and there it is uh iv president picture is it that easy let's hope so okay so we've got ourselves the entire list adapter recyclerview adapter very nice let's go back to main activity and we've got a problem here it says we've got one item in our in our parameters and we need to send two so let's take a look at what we created and the constructor that went with it because this one doesn't work so the recyclerview adapter we created a constructor and it says we need the president list and then the second item was the context so that's what's missing so back into our code here we're going to put in a comma and then the context of this thing so this is main activity i could just put the word this i could also be very specific and say mainactivity.this and that will tell me my context and then it says it'll set the adapter i wonder if this will really run let's see what happens i'm going to run it and then probably have to fix a dozen errors but here it goes man i'm feeling nervous here this is a lot of code and here it comes okay so i've got a problem i've got george washington 1788 and then i looked down in my logcat to see what went wrong so yes too much to hope for okay so i'm looking in here and i've got a problem that says trying to get something permission denied well i know what that means at least i should know what that means is that i need to set up some permissions there was a problem here it says you had a problem load glide failed to load a resource do you know why it's because we tried to get it from the internet so i forgot that i need to tell the app that we have specific permissions so let's go to the manifests and at the very top here we're going to add a permission okay so i'm going to put in a little bracket here uses permissions and the first one is a suggestion let's use the internet permission and then we'll close it off could it be that simple let's run the app and see if it works okay we've got george washington and uh do we have uh john adams now the the picture quality is really big right and i can kind of see some other stuff so it looks to me like i've got most of what i want except the president pictures are too large and they don't fit well that's probably something in the layout so let's see if we can fix that i'm going to go back to one line president and here is the picture size so let's see if we can adjust that is there a size that we could fix so there it is layout width wrapped content let's put in something like let's put in 90 dp and then the same thing for this go let's go 90 dp and run it again okay look at there we got george washington and we got john adams and looks like my i've got another problem here the the size of each line is really large it doesn't quite match and so i'm not quite there yet i think i need something with an adjustment on the height so it looks to me like the problem is that the entire layout is taking an entire screen height so let's choose the root here and look at the layout height it says match the parent so the parent is the entire screen and i don't really want to do that i want to do wrap content so that way the height of the layout as you can see just shrunk down to be exactly the the number of pixels that we need for there what's the problem here it says it's not horizontally constrained well let's let's make it horizontally constrained i'm going to put a left and a right and that little exclamation mark is gone and let's run the app again and cross our fingers this time hopefully we'll see everybody in a reasonable size space and will you look at that this is like amazing we've got ourselves all of the presidents lined up here vertically all the way down to ronald reagan it looks like we are in good shape so there you have it we've got ourselves a adapter that contains multiple items in each line and that's that's the recyclerview now what we need to do next is we need to start implementing some of the other features such as adding another item to the list saving this list in a central location where it's easy to get to sorting the list and so if you came just to see the recycler view and how that works you'll you'll get something out of this however if you're going to sort the list you're going to notice that things don't work as like you expect so that's coming up here but thanks for sticking with me and let's move on to adding a new president to the list in the next video in this video i'm going to show you how to create a global accessible list of items and so we're working on a recyclerview app but if you just came to see the global list then stay tuned so in this application we have two different uh views we have two different screens we have the main screen which shows all the presidents and then we're going to have a data entry screen and so the issue that arises is we're going to be sharing a list of data between those two screens so the presidents obviously want to show every president on the list and then the data update and add screen is going to also add an item to that list so the question comes up where do you store that in the previous edition of this tutorial we just had the first screen the one with the president list on it that had the list in it and we're going to find out that there's several ways that you could share that data one way is to create this global variable way so that's that's the process that we're going to show here an alternative if you want to get into it is to share that list with a database so you would have database access rights for each of the activities and each of them could save and read from that list of data so either one of these solutions will work and we're going to do the first one which is the global access list but if you would like to see how to use sqlite which is the android database you could see the tutorial that i'm going to suggest here at the top or down in the comments below so if you'd like to use the database then go ahead and check out those links otherwise we're going to continue on to create this global variable in an application so let's look at our code and see where we currently are so in the activity we've got right now the main activity we have the recyclerview and i think it was some down here we created a function or a method called fill president list and so this is where we initialized the list where did we save the list the list was saved here as a class member variable so near the top of the class now what i'd like to do is to make that accessible to other activities so could you make this thing public and then share it that way here's here's a way that might be recommended for you so we're going to create a new class and we're going to extend the application class so i'm going to the java folder and choose new java class and i'm going to call this thing my application and it's just going to be a generic class to start with now we're going to do extends and then the application is the this is the next thing so this gives us some special abilities it allows us to create a globally accessible class which is kind of what we're after now this is not going to be recognized unless we manifest put something in the manifest so let's go into the manifest area so now here in the manifest we are going to add a new property inside of the application brackets so i'm going to type in the word name so i'm looking for android colon name and then it gives us a suggestion it says perhaps you wanted to call it my application well it wasn't that handy that's exactly what i wanted to call it so i'm going to save that so if you forget this step you'll get all kinds of strange errors so make sure that you go into the manifest and say that your application is now got a name all right so that's like an id number really so let's go back into our code here that was uh my application and now we're going to add some new variables so the variable that i'm interested in sharing globally is the list of presidents so i'm going to create this as a private and type list and then we'll call it presidents list and it's the new arraylist so i could have copied this directly out of main activity it's the exact same line now i'm going to change one more thing on this is i'm going to add the word static here now the reason why i'm going to put static is because i don't want this to change from one instance to another so static keeps the list well static now we're going to borrow some code here from main activity so let's come back to here and let's go down to the initialization so i'm going to just cut this out now i'm going to cut it ctrl x and then come back to our my application and let's create a new new new method there so we're going to be using that in a second what we need next is a constructor so i'm going to put a constructor in by choosing generate so right click and generate and a constructor and there's my application so what do i want to do when my application starts well i want to do fill the president's list and that's pretty much all i need to do so i could just copy all these lines and put them into there that would work as well but i've got a extra helper function now i'm going to be adding and perhaps removing these presidents from the list so i've got a list of id numbers that goes from zero to nine how am i going to figure out what the next one is well i should probably keep track of what the highest id number is so i'm going to create another property so the property i'm going to make is an integer and i'll make it static as well and we'll call this thing next int and since we know that we're going to have item 10 as our next guy let's set it to 10 right now so i'm going to be using that property whenever i add a new element to the list so the next id number will be 11 12 13 and it will never go lower so we can skip id numbers they don't have to be sequential now the last item that needs to be done are the getters and setters so i'm just going to do the generate command again and i will generate all of the getters and setters that i need here by selecting both and clicking ok so we've got ourselves a globally accessible list well supposedly now how do i ex get that so i'm going to come back in the main activity now and we've got ourselves uh this issue of fill the president's list let's get rid of that and i'm also going to define the president's list differently so to make this work i'm going to have to make another reference to my application so i'm going to say i'm going to copy this i'm going to say my application is going to be the variable name and where's it going to come from well i need something called get application and let's see if that'll work so it says you have a problem we need to put in a cast statement so let's let's see what happens when i choose cast there it is so now it says get application is going to be of type my application okay so now i have a reference to my application okay so now i have this president's list defined and i'm not going to sign the value yet i'm going to just put in a semicolon but down here in the uh method called uncreate that's where i'm going to create it so we before i had fill president's list i think right here this is going to do the same thing though it's going to say presidents list is going to be equal some new values so i'm going to say my application dot and i'm going to type in get i'm hoping to get presidents lists presidents there it comes it's slow this time but get president list is one of the methods so this should fill the presidents with the value from the global application very nice let's test it out of course i'm going to run it and see if anything broke all right the application is up and running and it looks to me like it's filling in the president's correctly so even though the data source is from a different class we still get the same results at the end now the next step is going to be able to access this list from another activity so we have a second activity in our problem here it's called add or edit one and let's show the layout so that way you know which one we're talking about here so the layout is for adding or editing one more item to my list so let's choose the activity add edit one and open that up and let's see what that is supposed to do you can see from the layout that is it's looking to create a new president and so this can also access the same data so let's go ahead and code that i'm going to add edit one and we're going to get a list of the presidents here so just like i did in main activity i can create a variable here that can hold all the presidents so i'm going to make a list of type president and call it president list says the local variable to this class but we can get the values from the main class we need to get something from my application so let's get a my application and we're going to make this as of this get application and once again it's going to force us to create a cast so we have my application coming from here then in the uncreate activity we can say the president list is going to be equal to myapplication.getpresidents uh-oh is it going to work there it comes good all right i don't know why it doesn't show up immediately but we've got ourselves the president's list so now what do we do when we get to the part down here where it says click ok well i'm going to put in some comments so we know what we're after here so here's the plan of what we're going to do i'm going to create a present object based on the things that the user just filled in for us and then we're going to add it to the global list and then finally go back to the main activity with the navigation so that's already done with the intents that you see on the screen so there's some things that we need to do to make these accessible so i have three different fields where they're getting data from the user so these are called et picture url and et person name and something like that so we've got three different we got name date and uh image we gotta put those into our activity so let's define those at the top of the screen here so i'm gonna define three different names that match up pretty closely to those edit texts so we're going to have the date the president's name and then also his image url so those are all edit texts they're going to be strings so down in the uncreate section we need to define these values we're going to get them from the layout so each of those is going to use find view by id and so we're going to have date of election president's name and the picture url so those should all be defined now so one of the items that we're going to have to create for a president is his id number and remember in the global class i created this field called next id so let's get that first so now we're ready to create the new president so that's literally what i'm going to call him his new president and we're going to use the constructor to create him so the constructor has several things first of all it has an id number so i know that that's going to be next id next is going to be his name so let's go to our edit text president's name and we're going to get the text from that okay the next thing is going to be his date so the three items that i'm trying to gather are his name his election date and then the image url and so all of these are supplied by the user in this form now when i'm finished here i still have a red line so the compiler is angry at me because something's not right it says if i hover here it says i was expecting a string and an integer and a string and you gave me three things called editable what in the world are you doing you idiot don't you know that those are not strings well i can fix these by adding the dot two string method at the end okay is that better now good grief you're so picky let's see if that helps so i'm gonna put two string two string and then two string now there's gonna be a problem with one of these and let's see if you can figure out which one it is okay are we done now let's go and check it out again it says you still got one wrong the middle one is not a string you provided me a string and i wanted an integer okay so yes the date is supposed to be an integer okay so how are we going to fix this one here so i'm going to type in the word integer and then i'm going to say get an integer from a string so that's what i'm looking for so let's do get integer and parentheses around it and finally the errors go away whew okay now there's a real big problem here is that we're just assuming that the user fills in the details of the form correctly if they leave something blank we're going to crash probably because we're going to have no values but let's let's worry about testing and validation in a minute let's see if we can test this to see if it actually runs first okay so the first thing is create a new president now we need to add him to the list so we have this president's list and i'm going to do add and then we're going to add the new guy the new president and then finally we go back to the main activity okay so we're going to add a new guide to the list however i made a mistake i should update this item here so we have the next id if we add a new president we need to increment the next id so let's say my application dot set next id and we want to do what currently is the next id and add one to it so let's see if that will bump that up by one all right let's see if we can run this and find out if we can add a president to our list okay it looks like we got an app going let's go ahead and add one and i'm going to get a picture and then put this person in so i'm going to swipe over here to the wikipedia article where i was getting these photos before and let's uh scroll down and let's get a new president here so hey there's barack let's try him i'm going to right click and choose copy image address and let's see he was elected in 2008 so let's swipe back to our app and i'm going to put in here barack barak and then his date was 2008 and now to paste here i'm going to click again do i have to do a long click how do i do this double click there it is a long click apparently is what i need okay and then that paste item comes up and there is the picture of barack obama hopefully that one works let's click ok and the app crashed okay so i didn't do it right obviously let's go and check out the log to find out why it says here uh we have a problem on line 41 it says int value on a null object reference okay so obviously we didn't get anything from the value at that property so it was empty hmm so i wonder if get integer is the right thing i'm going to try parse integer let's try that parse int and we'll do that okay so here we go we got the app up and running again i'm going to add one let's type in the same guy barack and i'm going to put in 2008 and do a long click on this thing to get paste and i think they've got them all filled out let's click ok and uh it does look like it came back to the main screen here's the test let's scroll down what are we going to get at the bottom so ronald reagan is there anyone beyond ronald reagan there is not so why didn't that oh there he is okay i'm sorry so barack did show up he just took a little longer than i expected and we've got a new president so this appears to be working now in the next video we're going to create an edit option so when i click on abraham lincoln it's going to let me go ahead and change him so hang around and we'll show you how to create an edit form hey what's up and welcome back to another application tutorial we're trying to create an android app that does a list of presidents so far we've done some of these things already we've got a list what we're going to do now is we are going to create the ability to click an item and then edit that item in a second screen [Music] so to make this happen we are going to have to put a click listener on the items that are on the left side here and then we're going to have to sense that we've edited an item instead of created a new one when we get to the right side so there's going to be code on both ends of this arrow so the first thing i need to do is go into the recycler view adapter and you thought you were done with that however we're still going to add some more so the item that i'm looking at here is this on bind view holder and we're going to be able to assign a click listener into here however we do need references to the layout that is this is referring to so i'm going to go down to the bottom here where i have the my view holder class and i'm going to create a new variable called it's of type constraint layout so i'm going to give this a name and call it parent layout so now for the parent layout as you might recall we we did define that in the layout so let's go to item view oops try that again dot find view by id r dot and let's go to id and let's see if there was something about layout so there's one line president layout that's the guy okay so we have a reference to the layout of our item okay so now that we have the parent layout as part of our view holder let's come back up to the binding section here all right so the next thing i'm trying to do is add a click listener so let's go to the holder property that we just defined dot parent layout and we're going to choose set on click listener and then of course just like we do with buttons we say parentheses new on click listener so as you might recall we use intents if we want to join one activity to another so we're going to create a new intent and the first item of the intent has to be the context which has been passed into us as a parameter so we'll just use context and then we can also tell it where we're going which is add or edit one so that's uh that's part of it now then we need to start the activity okay so i tried to do start activity but it's got a problem here and what's the problem it says here you need to add a qualifier of context okay if that's what you say that's what it is so we'll start a activity from the context and then it says which one do you want to do well we've only got one intent all right so that will send us to the form but it will not tell us how that form should be filled out because this is really an edit form now we're going to turn it into an edit form so we need to do something called put extras so just before we start the activity we're going to put extras so the item that we want to send along to the other form is the id number of the person that we clicked so we're going to say go to the president's list get the position and get the id number for that person at that position and we'll send it in an extras package so when we get to the other end we're going to have to get the extras out of that bundle and then maybe uh display the contents of the president there okay so this should work let's uh let's go back to the other form now and we're going to intercept this item so when we get to the uncreate method in the add or edit one we can get the item that has been sent to us so let's come down after we've defined all of these things and let's get the intent from whatever may have been sent to us also i'm going to create another variable at the top and we'll call it an integer and we'll just call it id so this is the id number of the person we're working with so let's go back down to here and we're going to define id equals the intent and we're going to say get the extras so we're going to get an int extra of item called id now what's the problem here it says we got got a red line it says it expected two arguments and we only got one okay so the the second argument is the default value so i'm gonna put negative one if if the intent doesn't provide any value then we're going to assume a negative number so that means we can get to this form either in edit mode which is where you get a positive number for the id or if we don't get any positive number for the id then we assume that we were just told to create a new president and have a blank form so this form serves two purposes both create new president and edit the president so i'm going to check on the id value if the id value comes in as greater than zero then we are going to assume we're editing the president and if it comes in negative one then we'll have to create a new president uh this is going to be wrong greater than or equal to zero is more correct because president washington has value zero so we better make sure that we include him he is a valid president next i want to loop through my presidents and find the one that we're after so i'm going to use a for each loop well really for each loop doesn't exist in java but it does something like a for each loop so it it does it does give me type ahead predictability so i'm going to say for each p for each president p in the president list we're going to do a test so now we're going to use the value of the id to check to see if we can find a match if somebody sent me id3 for instance which would probably be what i don't know lincoln or somebody then we can assume that our new president this this value called president is actually equal to p so this will assign a value if there's a match so now we can assign all three values of the president's object to fill in the form so the president's name the president's date of election and then the image url all can be gathered from this president object so these should all fill in correctly if there's an id number that matches i want to check something else here let's go back into the edit layout and you notice i created a text view at the top here let's go and choose the design mode so this thing was called president id number and so this is what i had in mind when i created this if we're doing an edit we need to show the id number up here of the president so let's go back to our edit and i don't even think i put that in my list of items that i cared about so i'm going to make a new reference to this object so it's a text view and we'll call it tv preside now down in the list of things where i'm assigning values i also have to do that so let's do tv press id and we'll find it so it's called president id number let's use that okay so now we can add that as one more item to be updated here so let's do president id number set text and it's going to be called the id value that was passed to us hey great i think we've got ourselves the president's uh values that should be updated now what do we do in the else statement so in the else statement it's we're going to create a new president so we really don't have to do anything i'll just kind of leave it blank now let's go down into the ok button so the ok button is assuming that we're always creating a new president well that's no longer the case now we have an edit form plus a create new form so we're going to have to check on the id number to see if that's already been assigned so here's where our check comes in we're going to say if the id number is set to a number greater than or equal to zero then we'll assume we're in update mode if we're if we don't have that if it's a negative one then we're going to have a new president so let's see i can take the code which was for creating a new president and let's go and copy that out let's cut it out actually ctrl x and move it up so there now it fits inside the else statement okay so how are we going to do the update well the update is kind of like the new guy we're going to update an existing president so to make this happen we're going to define a new president and then we're going to replace the one that's in the list with the new guy so to create this new president i'm just going to go look at the data entry form and get the three strings that are there so his name his date and the url now the middle one obviously doesn't work because it is an integer we've seen this before so we're going to do a interpretation okay so we're going to have to have an integer.parseint to make this happen so now we have this updated president object we just need to substitute the one that needs to be swapped out okay so we defined a new president and then we are going to replace the one that belongs to his id number okay let's test this out see how well it runs all right so let's pick on john adams let's see if we can update him and it crashed okay so we got a problem here and let's go look at our logcat to find out why so it says here uh line 51 is what caused the problem and the president's date we were trying to get this date of election and silly me i said that was a an integer we have to change that to a string so let's uh let's put that in there let's go to what is it string dot value of integer okay let's try that okay try it again and this is so picky okay we're up and running again outside john adams again and it crashed again okay now what it says line 53 this time what is line 53 it says you're trying to set the text with an id oh my goodness let's do a two string oh my goodness the same problem twice let's go ahead and run it this time maybe it'll work okay cross my fingers john adams here we go and poof there's john adams now we could uh change his date or something i'm going to change him to be 1700 that's obviously wrong and click ok and now you can see john adams 1700 shows up i better fix that i what was that again it was 17 was it 96 or something like that i forget my history is bad there it is we've got john adams updated twice so the next part here is to do sorting so we're going to add a little menu at the top right corner that will allow us to sort the list in alphabetical order and in chronological order so if you want to see how that works stick around hey what's up welcome back in this version of the application that we're building we're going to do sorting so you can see we have a list of presidents and we're going to add this menu to our application that allows us to sort alphabetically forward and reverse and then the ascending descending refers to the date [Music] all right so the first thing i want to do is i want to add a menu to my main activity here so how do you put a menu on here well we go into the resources and let's do a right click and choose new and we are going to create a resource directory so it doesn't come with a menu resource directory to start with by default so the type we're looking for is menu and the directory name is also just fine to be called menu so let's just click ok now we've got the menus choice down here let's do a right click on the menu now and choose a new resource file a menu resource file and here it is it's called let's call it sort menu and i can't use capital letters let's try sort menu and let's click ok so here's the menu that we're going for we're trying to do uh a to z z to a ascending and descending so four menu items that we're going to add here right now so to put a menu item here let's just do a menu item and drag it down to about there and it says menu item okay well let's give it a name so the title let's call this thing a to z and let's give it an id so i'm going to call it menu underscore a to z let's see how's that for a name let's drag in another one and do the rest of them okay so i'm going to drag in all three of the menu items and give them two things first of all give them a text a title and then also an id so for the id i'm going to use the word menu as a prefix underscore and then a description okay so this creates the menu items so uh you could add icons and things up on the top but for right now we're just going to keep it simple now let's go into our code and see if we can make this actually happen with some clicks all right let's get back into our main activity and we're going to have to set up some things here at the top so let's create a resource called menu and let's just name him menu and let's go ahead and alt enter to import that so now we need to create some events that assign values to those uh the menus how do we do that well we've got some we've got some uh events like uncreate that are already defined for us are there any other events that uh that an activity can do well you bet there are let's go up to the word app activity here app activity compat and i'm going to press ctrl o for override and look at all that stuff these are all the activity methods that we can override so i'm looking through the list and then what i'm looking for is on options item and so if i can spell it right we'll get to there so there is on options item selected so i'll double click that and i've got a new override event here in my actions i'm going to need another action here so let's go back up to appcompat and press ctrl o and this time i'm looking for create options menu so there it is on options create options menu okay so these two down here are going to work with my menu so what we're looking for is called a menu inflator okay the inflator we're gonna get it from the menu inflator then the next thing is we're going to assign all of the items in the layout to the class that defined at the beginning called menu all right so this thing here we could return it i think we can just put in the word true so it's always true now what are we going to do to actually make those click items happen that's what the second thing comes from so when we get items selected that's like a click event so now that we have our menu object here full of four items we can decide what we're going to do when we click on each of those so what i'm looking for are a switch statement the switch statement is used because there are four different choices we could use if statements but switch is the right thing to do here because we don't want four ifs in a row so i got a switch item and it says you can't really use that so what i want is the item id so in the layout there is some long integer value that is associated with each menu item now when i if i hit any one of those four then i can do an action so let's say the person selects the first menu item so that's going to be case case of what well if the the selected id matches r dot id dot menu a to z those two numbers will match if that's true then we want to be able to sort so we don't have a sort routine yet so i'm just going to put in a comment that says sort from a to z we need to have a return statement so i'll put in return true just to test things out i'm going to do a sort and we're going to put in sort from a to z as my text and let's see if that works now i'm going to put in the case statements for the other four all right so i'm going to put in the other three i guess we'll call them three cases so case number two is if we choose menu z to a so z to a is you know reverse alphabetized and so i'm going to put in the toast message this is z day and then for the date we'll do data sending and we'll put the text message that corresponds with it and then the same thing for date descending so when we're all done we should have four different cases that we could handle using the menu click and so let's let's test it out these are not actually going to sort the list yet but it's going to give us a toast message to tell us that it's going to sort them all right we got the app up and running again and this time if you notice carefully there's a menu so let's choose the menu and let's select one of them let's try the first a to z i'm expecting a toast so watch the bottom of the screen and uh bang it doesn't work oh great let's go check out the log cat and see what caused the problem so is there an issue so it says here java line 89 and what is uh line 89 so 89 i'm predicting is going to be something with a toast so my application why did it use that let's let's try this let's do get application context instead and i think that will probably be better i think we could also just put in mainactivity.this yeah that should work so there's multiple ways to get your context and this one here will probably be the one that i use most commonly sorry about that okay let's see what happens here i'm going to run the menu and watch for the bottom let's try a to z and sure enough it says sort a to z and i would bet you that if i choose another one we're going to get something like sort ascending by date perfect okay now we need to go and actually perform the sort which is going to require some java skills so come back and let's do the sorting and implement them in our class hey there welcome back we're going to do sorting on our application as you know if you've been around we've done some recyclerview demos so we have a list of presidents and we are able to edit them and now we want to be able to implement these sorting items so in the previous video we created a menu that says these four sorting options and now we're going to actually code the sort and that's going to require some java skills so let's get started [Music] okay so let's get back into our code the president class is where we're going to have to do some coding to make it sort so if you remember way back at the beginning part of this tutorial we created a president that had four properties he had an id a name a date of an election and an image url so the two that we're interested in sorting on are the name and the date so that's we're gonna have to do next so to make this happen we need what's called a comparator okay so let's set up this comparator so we're gonna make it public and static and we are going to say it is a comparator on the class called president now you give it a descriptive name next so president name a z comparator is what i choose so there's no doubt about how we're sorting here we're going to have four different comparators and we're going to make a new one and it automatically generates a a method called compare so the the two things here are going to be president and t1 i don't know why they just call it that let's call this thing let's call them p1 and let's call this thing p2 so you think we have president one and president two and we need to send back a value an integer if they are equal they get a zero if it is a positive number that means president one comes before president two or the reverse so the method that we're trying to do is return a value and we are comparing only one of the four properties here the property of name so we're going to say return p1 get his name compare it to the string of name from p2 so whatever those two compare to we're going to return it all right so that is the first comparator so i'm going to put in a semicolon to get rid of that error now the another other comparator is going to be the exact opposite as far as the order so i'm going to copy and paste this guy and rename it so instead of calling it president name a z parrot comparator we're going to call it z a so just transposing two letters is going to reverse it now we've got president 1p p1 and p2 so we're just going to reverse the comparison so i'm going to say take p2 and compare it to p1 so whatever the first one was this one is a mirror image so if you compare two presidents we're going to do reverse order this time we might also keep going with the comparators i'm going to name this one a different one so let's call this guy the president date ascending comparator and so now we've got president p1 president p2 again this time instead of comparing the two strings we're going to compare two numbers so when you compare two numbers all you have to do is do a subtraction problem so if you send back a positive number that means the first one is bigger a negative number means the second one is bigger and then the zero of course means they're equal so there is our president's date ascending comparator now the next the next one is going to be the descending so see if you can figure it out you see a pattern working out here if not just watch me i'm going to change this last one to be a descending comparator so i'm going to copy the last comparator paste it and rename it and instead of changing p2 or p2 is going to be the first one and then p1 is the second one all right so we've got comparators now this is a nifty way that you can compare two different objects based on one property of the object okay so where do we actually want this to happen when do we call these comparators right now we've just defined them so let's go back into our main activity that's where the menu items are and as you recall in the previous video we created these menu items with click listeners and all we did was put a text out so let's now use these comparators okay so here's how this works so we're going to use the keyword collections so collections refers to any kind of list and dot sort we're going to tell it which items we're going to sort so we're going to collect we're going to sort the president's list and then we're going to call the class president dot and you can see that the president name a z comparator is what i'm choosing so can you do this sorting on the rest of these i bet you could go ahead and try it and if you want to watch me go ahead and see what happens here okay these are going to be very similar so i'm just going to copy the items and paste them and just change one detail so the only detail is the comparator so this is going to be president this is from z to a so let's go to that comparator the next one is about date ascending so we're going to do that is a different comparator the ascending and the last one is the descending okay something's not going to work though and instead of just running the application and showing you what doesn't work i'm going to fix it right now so we have an adapter that is now out of date the adapter originally had a list of presidents in order as we assign them and now we've just scrambled the order we've just we just sorted it so we need to tell the adapter that we've been updated so this is a real simple command but it's it's really important you tell the adapter dot notify data set changed so the data set is now a different different animal so you better just refresh yourself so i'm going to copy and paste this thing okay this would be really nice for ran let's try it out and see what happens when i run my application and see if these comparators do what they are advertised to okay here comes the list so they're in default order right now let's go ahead and sort them so i'm going to choose from a to z and look at there abraham lincoln is the first guy and dwight eisenhower and that's pretty amazing so we've got ourselves a sorted list let's see if we can sort it the other way so let's go to z to a and let's see who's first let's scroll back to the top and it should be ulysses probably he's the did i put grant in here yep ulysses grant is the first one on the list now and uh let's see date ascending and we come right back to where we were george washington is the first president okay so we've got ourselves a whole bunch of items let's go and see if we can edit somebody george washington 1788 i'm going to put it 1789. and click ok and did he update oops i clicked somebody where's george washington let's go sort again by date ascending and here it is i've got a problem i've got george washington in there twice so my edit field is not working correctly but i'm close so if i come down and look through the list when i click the ok button i see a problem so here is the statement that i typed that says when i click ok i'm going to check to see if the id number was greater than or equal to zero well i didn't actually do that i don't know what this means greater than negative zero that's probably not really what i meant so greater than or equal to zero and let's give that a shot let's see if that will let us update properly now so you probably didn't have that error at all it was just my mistake okay so here we are we're running the app and let's try john adams again i'm going to click him and let's change his uh date so i'm going to change him to 9999 and click ok and john adams now has a new year for nine nine nine nine all right so congratulations you came to the end just think of all the things that you've learned here you've learned how to do a list adapter you've learned how to create a glide using uh putting images in with glide a menu you've learned how to do sorting you've learned how to do intents and pass data between each other you've learned how to set up a global accessible list of presidents what would be next maybe you'd want to add a database you could use sqlite if you want to use a local database i have a tutorial for that you can check that out or you might want to save it online where you would put it in a rest service and save it on a server somewhere on the internet or you might want to use google firebase there's lots of ways that you can persist the data also please do not click subscribe i don't want any more people watching these videos there's already too many people subscribed to the channel so don't click subscribe don't click the bell and never watch another video here again please and have a good time with your android development
Info
Channel: shad sluiter
Views: 9,271
Rating: 4.969697 out of 5
Keywords: Recycler View Android Example, android recyclerview, android recyclerview tutorial, android recyclerview and cardview tutorial, android recyclerview onclicklistener, android recyclerview design, android glide, android menu, android intent, global variable android, myapplication android, recyclerview android, recyclerview android studio, recyclerview, recyclerview onclicklistener, recyclerview android tutorial, recyclerview onclicklistener to new activity, recycle view android
Id: FFCpjZkqfb0
Channel Id: undefined
Length: 104min 27sec (6267 seconds)
Published: Mon Sep 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.