‫Welcome back.
‫We created the data class.
‫Now we need to create the adapter.
‫Go and create a new Kotlin class.
‫Name it as my custom adapter.
‫This adapter class acts as a bridge between the data source and the grid view.
‫You need to create an adapter to populate the data into the grid view.
‫The adapter acts as a bridge between the data source and the grid view, and you can extend base adapter
‫as we done before, or use other adapter classes like array adapter or cursor adapter.
‫In this example, I'm going to introduce a new type of adapters called array adapter.
‫I want from you to focus with me, and the intention for using array adapter is to have diversity in
‫using adapters and working with adapters.
‫So I'm not going to extend base adapter.
‫Instead I'm going to extend the array adapter.
‫At first I need to add here the PRI in the primary constructor of my custom adapter do parameters first
‫the context object.
‫I'll use it in the layout inflator alt plus enter to import the context class and the private val grid
‫items.
‫It's of type list of shape.
‫I need to pass a list of shapes as a data source for this custom adapter, and here inside the array
‫adapter I'll use as a generic the shape.
‫So I'm telling go and create an array adapter holding the shape class as its data source.
‫Then I need to pass three parameters.
‫You can see it's suggesting for me context and resource int, context, resource int and text view,
‫resource id and others.
‫I need to use that one the context resource and objects.
‫It's of type list.
‫You can use an array, but in this example I'm going to pass a list to the array adapter.
‫You can use array or list context zero and grid items.
‫Let's break this line of code down.
‫The array adapter constructor is used to create an array adapter that will be responsible for adapting
‫data of type grid item to the list view or grid view widget in the Android UI.
‫The context.
‫This is the context of the activity or application.
‫It's typically the current state of the application or the activity that is using the adapter.
‫The context is required to access resources, layouts, and other application specific information.
‫For that, I need to use it in the layout inflator to access the layouts.
‫Resource ID for the layout is zero.
‫The second parameter represents the resource ID for a layout file that will be used to create a view
‫for each item in the adapter.
‫However, in this case, zero is used because you are inflating the views manually.
‫In the grid view method of the adapter, you are not using a predefined layout resource, so you provide
‫zero as a placeholder.
‫So whenever you create a custom grid layout and you need to inflate this custom layout, you need to
‫pass zero as a resource because you are not going to use a predefined layout resource.
‫The third parameter is grid items.
‫This.
‫This is the list of data objects that the adapter will adapt and display in the list view or grid view.
‫And in this case I'm going to use a grid view.
‫And and I'm going to use a grid items, which is a list of shapes where each grid item object contains
‫information about an image and text.
‫So when you create an instance of array adapter and you pass the data class, you are essentially creating
‫an adapter that will convert a list of grid items objects into views that can be displayed in the grid
‫view.
‫The layout for each item is handled manually in the Get view method of the adapter, where you inflate
‫the views and bind the data to them.
‫The second step is to create a new class called View Holder.
‫I want from you to focus with me in this video and the creation of this custom adapter.
‫Here I am following a different strategy in implementing and creating adapters.
‫I'm going to use something called the view holders.
‫I'll create an internal class here.
‫Inside this class it's called inner class private class view holder.
‫Then I need to use late initiate var image view.
‫It's of type image view.
‫And late initiate var text view is of type text view.
‫In the next video we're going to deep dive into this Viewholder.