Adapter Views and Adaptors

Jason Fedin
A free video tutorial from Jason Fedin
Instructor / Software Developer
4.4 instructor rating • 6 courses • 97,752 students

Learn more from the full course

The Essential Android O Developer Course (Java)

Learn everything in detail on how to write apps in Android O using Java

38:44:50 of on-demand video • Updated May 2020

  • Create and Run your first mobile application
  • Write Efficient and High Quality Android applications
  • Understand how to compile and test Android Applications using Android Studio
  • Learn all aspects of Android development from beginner topics to advanced
  • Learn how to Design and Implement Graphical User Interfaces in Android
  • Learn how to integrate databases into an android application to store persistent data
  • Learn how to submit apps to the Google Play Store and generate revenue with ads and in-app purchases
  • Learn about using Fragments for dynamic User Interfaces with multiple screen sizes
  • Understand how to implement mobile apps using the Java Programming Language
  • Learn Many more Android concepts!!!
English [Auto] Own welcome you want to continue our discussion on graphical user interfaces and specifically in this lecture we're going to talk about adapter views and adapters and adapter view is a view object that displays data via an adapter. It's very efficient. It can display millions of items. It contains a scrollable list essentially depending on what view you use. But you can store a lot of stuff. And it's very very efficient and very very porns very well you should use an adapter view when the content is dynamic meaning it changes. You can also use on static data but if you do have dynamic data you should definitely use an adapter. The adapter acts as a link between a data set in an adapter view so it looks like this. You have your data source the data source some examples of data sources are a cursor. If you're reading from a database an array list if you're populating list you can also use a normal array. You have the adapter in the middle which acts as the middleman and adapts the data to give you some examples of an adapter view our list view grid view and spinner. All of these views are going to use an adapter to populate data in the view. So we'll go through some examples of how you can create a list view populate data on it. Same with views same with the spinner but this is the essential design here. Or you have your adapter. And for those of you that are familiar with programming and adapter is also a popular design pattern that allows to components to be able to communicate. And this is what is happening here you have your data source data that needs to be inside of a particular view and the adapter can take that data and put it into The View. So the other part of the adapter view is you need the adapter and so the adapter is going to be used in most applications in the form of a list a girder stack just like the examples demonstrated you have a list view. You have a good view and then the spinner also x x is a stack it's responsible for retrieving the data from the data for generating view objects based on that data. And that is what it does and that's how it acts is that middleman takes the data turns that data into view objects the generated view objects are used to populate an adapter view that is bound to the adapter. So for every list view you need a list adapter for every good view. You need a grid adapter to create an adapter you're going to need a data set and you're going to need a resource file common data sets are arrays lists and cursor objects as demonstrated in the previous illustration common adapters are the array adapter and the simple cursor adapter. So if you wanted to use an array adapter to populate a list view for example you would do the following steps. You would first create the data in a resource file. This isn't always required. But again we want to use resources as much as we can in all of our projects because resources we know now that they separate the code and they also allow allowed to be more usable. So it's a good idea to reserve resources so if you have static data you can store data and the static array in the string array in a resource file. It's not. You could also just create an array inside your Java code and populate the array adapter that way. There's also a third way if you create a custom adapter which will talk about a socket but the first thing you need to do is you need to create that data set. And for our demonstration we're going to create in a resource file a string array. You then need to create the layout resource file this layout resource file is going to contain a text view object so when you when you create the adapter's the adapter's and the adapter views only store text to use. So you have to create a resource file that stores the text. If you have a adapter of you that you want to store something different like an image or you want to have different columns you have to then create a custom adapter. But if you just have a normal adapter you're going to need to lay out a file that contains contains your text view object. Once you have the text you are creating the resource file. You then create your adapter you do something in your Java code you create an instance of an array adapter and you say newer array adapter and it has to be of string type. It's a generic here. You pass in the context the context is the application going to show you how to do this. But the context represents the activity our data lay up a tax that would be what you created Step number two that would be your tax view object and then the string array the actual data that stored an array. In this case what you Creon step number one using a resource you then step number four you would actually create the adapter view use the layout editor. So you create your list view inside the layout editor the graph editor and you would drag it down into your layout and put it in there that way. So you have to have an adapter view in your layout file so you could have an adapter view such as List View spinner or grd view. After you've done that you have to set your adapter if you object to the data. So you use the adapter use the adapter view object so first you have to get the resource to list view for example and you'd have to use the find view by the method to grab the list view from your resource file from your lay file so you can pass the art ID doubtless view you cast that to a list Universalis you object. You then can call the adapt their method on that object and you can pass in the adapter that you created in step 3 and that is it. That is all you need to do to adapt static data to a list a list view. It can also be dynamic data. If you're reading from some source file or whatever the case may be if you're reading data from a web service it can also be dynamic data or static data if you want to use a simple cursor adapter which is the second way to adapt your data. This consist of data from a database using a cursor object and you would need to use this if you're reading data from a database and then storing it into a list view a spinner grid view the cursor that fits in between a cursor a data source from sequel like query. So when we talk about sequela databases and querying data you're going to use a cursor to get that data. And so you need that cursor object to to use and the cursor adapter. And then if you're using List view you would also use that cursor adapter so the cursor needs to lay out a template to inflate an item and then the cursor fields need to bind the views to the template. So for example if you had a database and you had specific columns from fields you would need to take those Frum's fields and you would need to map them to the columns in the list. And then once you have that binding of the views then you can you can do your setting and the data or so here's a little simple java code example this is going to make a lot more sense when we talk about databases and we use the simple cursor adapters but I did want to give you a brief overview of it since we are talking about adapter views and adapters you would you would create a string array for the from columns. This would represent the columns in your database and then you would map those columns to specific resources of of your list view. And that would be another way you would create your simple cursor adapter object and you would pass in the context your activity then you would pass in your resources for your layout. This would be the the list view resource. You then pass your cursor object your from columns your two views and 0 for your last parameter. Once you have that all set off your adapter you would do the same type of thing you did in the previous slide you would create your list view object probably using find you by or a helper method like this list view and then call the site adapter method on that object passing in the adapter and will provide. I will provide a demonstration on how to do this. Lastly I mentioned that the previous two slides for when for were were when you had dynamic data or static data but a specific column like a text if you're displaying a text text view. Well you definitely want to be able to display data in a list view or spinner and you may want to add icons you may want to have multiple columns with different types of views and when you need to do that you have to create a custom adapter. So when you have a specific representation other than a text view you're going to create a create a custom adapter in the way that you do that is the first thing you need to do is create a Java list with attributes in the list. So the Java classes are you create a Java class with attributes in a list so you would create a your own Java class an object that has maybe However the attributes would represent the attributes in the list view. For example if a list you had three columns displaying your name your address and your date of birth you would create an object class in Java with those three attributes. Once you have that class created with those attributes that map to the list you would then create a new template in SML multiple textures representing each attribute in the above Java class for example. Now these would represent the columns for example in a list view. If you had a spinner they would be each item that would display in the spinner. They don't have to all be text views you could have one column as an image. You could have one column as a bar and you could have another column as a text to you but you would have to have that access e-mail resource file that contains the actual views or or fields and if you that you want to populate then you would go on to Step Three would you would it you would create your own adapter by subclassing the adapter class. So if you are using an array adapter to store store data you would subclass the array adapter. If you're using a simple cursor adapter you'd have to subclass that when you subclass that class. You would have to then override the give you method to give you method is what it's called when it's populating the view. So it returns each row as if you object. And this is the conversion with the adapter. Now when you subclass the adapter you have to provide this implementation that give you method when you don't subclass the adapter the array adapter already has this implementation and automatically calls all that stuff under the hood. So you don't have to worry about it. But when creating your own custom adapter you have to implement this give you method and will provide examples for this as well. In the give you method you're going to translate the data item to have you to display. So you'll get some resources for example and from the resources your bind them to data and so forth in step 4 you're going to attach your own adapter to have you using this set adapter method just like you do in the previous sleights in step 5 Your populate that date into the list you list for you is probably going to be dynamic. So this is why you do the population after setting the adapter you could populate the data from the list view before Step 4 but it's typical when you're subclassing to populate the data afterwards. You would then add event handlers. We haven't talked about event handlers. Yeah I will provide a demonstration of them. But I did want to provide the steps and event handler is what happens if you click in the list view. You may want to do something you may want to retrieve that data and you may want to populate a database. You may want to go to a different activity or a different screen by if you select something in the list or if you select something in spinner. So event handlers respond to user interactions a user touches a button a user touches something in the list view. There's a method in the Java class that gets invoked. And you do something when they have that interaction. It's called an event handler. And so this was an introduction and overview of adapters and adapter views and the next lectures we're going to talk about implementing this. We're going to talk about implementing a list view with the with an array adapter we're going to talk about implementing a custom adapter for a list view. You also discussed the grid view. And finally we discussed the spinner. Those are the most that those are the three common views that use adapters. Thank you.