Welcome back.
In this video, we'll create the most important part of our application, which is the custom adapter.
So before we start, please pay attention with me and any doubt about the adapters, please refer to
the adapters section and the list of U.S. to better understand them in deep details.
I'll create a new Java class.
I'll name it as my custom adapter, and this custom adapter will extends array adapter and for the array
adapter here I need to pass the model class for our case and our application is a shape class array
adapter of shape objects.
The custom adapter extending array adapter in Android allows you to create a specialized adapter for
populating UI elements like list, view, spinner or grid view with data from an array or list of items.
And in our case, it's a grid view.
It provides more control over how data is displayed and allows you to customize the appearance of each
item in the list based on your requirements.
So I'm creating custom adapter because I need to display custom things custom items in the grid view
like the image view and the text view that we designed before.
So I need to display this layout for each item in the grid view.
So for that I created this custom adapter and I'm extending array adapter because my data source would
be array list.
So this class will be responsible for creating the view for each item and binding data to it.
And since our data source is an array list, I'll start with private array list of type shape.
I'll name it as shape or shapes array list.
And of course I need a context object to be passed from the main activity.
Our data source is passed as an array list and the context object represents the current state and environment
of your application.
It provides access to various resources and services that your app needs to interact with the system
and perform tasks.
And in this case, I needed to create an inflating view.
Now let's create our constructor.
Alt+ Enter Create Constructor.
I need to get these to.
And here we go.
I remove the context.
And here I need to remove this resource.
So the two parameters of the adapter that I need to pass is the array list and the context objects here
in the super.
I need to replace it with our layout that we defined for each item in the grid view grid item layout.
And the third parameter is the shapes array list.
So again, super is referred for the array adapter here.
And if we go to the array adapter class Ctrl and left mouse, you can see array adapter extends base
adapter and if we scroll down to the constructors, we can see I need to pass the context and the layout
resource, which is an N here.
We're using this constructor that we need to pass the context as first parameter.
The second parameter is the layout resource, which is referred for the item grid item layout and the
objects, which is an array list shapes, array list that we passed as parameter.
Then we need to create the view holder.
The view Holder class is used to cache references to the views within an item layout so that they don't
need to be repeatedly looked up during scrolling.
And for our app, I need to create two references, one for the image view and one for the text view.
For that, I'll start by creating the private class private static class text view shape, name, image,
view, shape, alt plus enter to import the image view and the text view classes.