Android Beginner Tutorial #8 - Custom ListView Adapter For Displaying Multiple Columns

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys this is going to be another Android studio beginner tutorial and in this tutorial I'm going to show you how to use a list adapter so let's adapter is for using a ListView if you and if you need like multiple columns or want to create like a custom view like this right here so you can see in this lists you we have like a person's name their birthday and in their sex and basically it's just a bunch of text views and a single list item view I'm going to be doing a two part series in the first part I'm going to show you how to build a list adapter just like a basic list adapter and in the second part I'm going to show you how to give get this scrolling animation in here so you can see when I scroll up and down it kind of loads the next item and I'm going to show you how to use the current best practice for loading a lot of list items because if you have a lot of list items it can cause lag and your main UI thread and there's a I'm gonna show you one example of a design pattern that helps you make sure that your list views load very quickly so let's start by creating a new project I'm just going to call it list adapter and we'll go next and that's fine use empty activity that's fine and finish okay we'll start by building our layout files so we'll go into activity main which is already open here and we'll get rid of this hello world text and the scare word is padding also and it will go to the design tab and let's just throw in a ListView so grab the ListView throw in the top left hand corner and go back to the text tab and we just need to give it an ID so let's call it with you and we're actually done inactivity main that's all we need to doing here so we can close it and now we're going to create the custom view for the list items so we'll go to create a new layout resource file and we'll call it the adapter view layout and I've already built this view ahead of time so I'm not going to waste time typing it out here so here it is I'll go over quickly basically we have a linear layout a horizontal orientation linear layout and we use something called a weight sum to distribute where the items are going to be in the horizontal layout so the first item is a text view and it's going to contain it is kind of the opposite of what you would think you you you say that the layout weight is sixty six point six so sixty six point six is two-thirds of a hundred but really this is only occupying one-third of the hundred so it's kind of like the opposite of what you would think you create a weight sum of a hundred say that this is sixty six point six percent of it which is two-thirds but really you only get one-third so it kind of sounds confusing but if you do it two times you'll definitely understand then the second item in the horizontal linear layout is another linear layout and we're going to say that this this occupies the rest of the horizontal linear layout here so our weight sum is 100 this one is set to sixty six point six this one is set to thirty three point three three but really this will occupy two thirds and this locked by one-third then inside of the linear layout here that's going to occupy two thirds we have another two text views which are just stacked on top of each other and this linear layout is different from this one because it's a vertical orientation this one's horizontal and for the naming conventions I just use text g1 text you to and text you three and that's all this this is basically the view that's going to get recycled inside of the ListView so each kind of unit in the ListView will have this regenerated one after another okay so we're done here we can close it and go back to main activity now we actually want to create another object class it's going to help us store the information that we're going to put in a list view so if we go back to our adapter view layout actually I'll pull up the PowerPoint enemy here so we know that we're going to need a space for a name a birthday and a sec so we want to create an object class that's going to store those variables do we need to store a name a birthday and then whether they're male or female so we'll create a new Java class want to call it person and then we just need three variables so we have named birthday and sex let me just hit alt insert to insert the default constructor select all these actually one name at the front there we go let me see all insert again go to get our insider select all the getter and setter methods and we're done now this is going to be our object class that stores the people that we're going to display in our list view so we can close that ok let's start before we create the list adapter let's start writing code in main activity so first thing as always is create the logs and then we go down to on create so we know we're going to have a list view and we can log on create starting because why not just so we know it started correctly and everything's fine now we're going to create our person objects I created a whole bunch of at a time so you have to sit here and watch me type out a whole bunch of people so here's all of our person objects we have the names of the objects they're just going to be denoted by the person name so you can see that like John matches John these matches Steve and then you have their birthday and then you have their sex I got some Matt and I got tired of changing all the parameters so I just copy and paste as Matt a whole bunch more times just so we can see that like the full screen of the demo gets taken out so now we're going to create an ArrayList and the ArrayList is going to hold person objects I'm going to call the ArrayList people list and you can see here that it takes person objects now we're going to add each one of these people to the people list once all the people are added to the people list now we're deploying will we need to actually create our person list adapter so if you remember in my previous tutorial on let's use what we would do is create an array adapter call it it adapter and then pass the context pass a default Android layout and then pass the list but this isn't that's not going to work in this case because we have like a custom view that we need to use because when you want to display multiple items per column so a regular list adapter won't work so we're going to create something called a person list adapter Clause after new person with and after and it's going to take the context and it will take our custom layout that we made and then it will take the people list and then we would just take our list view and then set the adapter to our adapter so this is red obviously because we haven't made this class yet so that's we're going to go ahead and do right now so just copy this go up and create a new Java class paste it in there and we need to extend arrayadapter and then pass our object type in here which is the main person and now we can start building our adapter we just need a couple things up top here we need our tag and our context now I'm going to press alt insert and include the default constructor we're going to use this one right here we just need to context the resource ID and the object actually we need this one sorry the object list so we just need to contact the resource and then this is actually going to be under a line and we know that because we are passing a context we're passing a layout resource and then we're passing our ArrayList right here so now you can see that this red goes away it's not going to work yet because we haven't actually written in code but all the red goes away because now it can identify this person list adapter object okay I'll put some comments up here for you guys now we're going to insert one of the override methods so click alt insert go to override methods and type get view so you can see it's that one right there just hit enter on it and we can delete the super we don't need that I'm going to give a little more space down here alright so this is going to be responsible for getting the view and then attaching it to the ListView so we're going to need a few things start we're going to need the name the birthday and the text now we need to create a layout inflator and then set our view to our layout inflator so we declare a new layout inflator and we're going to deflate it from the context and then we take convert view which is this variable up here which returns the view to inflator dot inflate and then pass our resource ID which is actually going to be this resource up here so we could just declare the resource up here and then set the resource in the default constructor and then pass that resource right here and then pass the parent and fall and this this is giving a notification here because it doesn't follow best practice design patterns but I'm going to go over that in my neck tutorial where I show you how to make sure that you're not that you're you're not trying to load too many list items at once because it can slow down your main activity thread this this will work it's just it could potentially cause problems if there's a lot of items in your list you but I'm going to go over that in the next tutorial I'll release it tomorrow okay now we need to actually declare our textview objects and then set the name the birthday msx2 the text use so the difference in how we would do this regularly as compared to right now is we need to do convert view and then find view by ID and then pass the ID of the textview usually when you do it in activities when you declare it you don't have to pass the view but because we're inside of this getview method we need to pass the view and now i need to set the text views to the string object that we declared up here so you'd go text you name step text and then pass the name same with birthday and then once that's all done we just return the view we just returned convert view and that should be good so let's run it and see if we get years all right looks like everything is working correctly we got all of our person objects in here and we can see that everything is set the way it should be so in the next video I'm going to go over how to add that sliding animation that I showed you in the demo before I started this tutorial and I'm going to show you the best practice way to if you build your list adapters because right now if I loaded it like I don't know a thousand objects and to my list you it would probably slowly op down like significantly so I'll show you how to avoid that in the next video if this video is helpful don't forget to leave a like below follow me on Twitter for notifications for when new tutorials are posted subscribe if you haven't already and thanks for watching
Info
Channel: CodingWithMitch
Views: 193,560
Rating: undefined out of 5
Keywords: android, learn android, android beginner, android beginner tutorials, android tutorial for beginners, how to make android apps, how to make a listadapter class, how to make a listadapter class android, listview with multiple columns android, android listview multiple columns, android multiple column listview, listview multiple columns android, android studio multiple column listview, listview multiple columns, beginner listview tutorial
Id: E6vE8fqQPTE
Channel Id: undefined
Length: 10min 27sec (627 seconds)
Published: Tue Mar 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.