WEBVTT

00:00.460 --> 00:01.510
Welcome back.

00:01.510 --> 00:06.970
In this video, we'll create the most important part of our application, which is the custom adapter.

00:06.970 --> 00:14.200
So before we start, please pay attention with me and any doubt about the adapters, please refer to

00:14.290 --> 00:21.250
the adapters section and the list of U.S. to better understand them in deep details.

00:21.250 --> 00:23.350
I'll create a new Java class.

00:23.370 --> 00:32.530
I'll name it as my custom adapter, and this custom adapter will extends array adapter and for the array

00:32.530 --> 00:40.810
adapter here I need to pass the model class for our case and our application is a shape class array

00:40.810 --> 00:43.240
adapter of shape objects.

00:43.240 --> 00:51.340
The custom adapter extending array adapter in Android allows you to create a specialized adapter for

00:51.340 --> 01:00.410
populating UI elements like list, view, spinner or grid view with data from an array or list of items.

01:00.410 --> 01:02.900
And in our case, it's a grid view.

01:02.930 --> 01:11.240
It provides more control over how data is displayed and allows you to customize the appearance of each

01:11.240 --> 01:14.000
item in the list based on your requirements.

01:14.000 --> 01:23.870
So I'm creating custom adapter because I need to display custom things custom items in the grid view

01:23.870 --> 01:27.560
like the image view and the text view that we designed before.

01:27.560 --> 01:32.600
So I need to display this layout for each item in the grid view.

01:32.600 --> 01:41.210
So for that I created this custom adapter and I'm extending array adapter because my data source would

01:41.210 --> 01:42.980
be array list.

01:42.980 --> 01:50.450
So this class will be responsible for creating the view for each item and binding data to it.

01:50.450 --> 01:59.090
And since our data source is an array list, I'll start with private array list of type shape.

01:59.090 --> 02:02.360
I'll name it as shape or shapes array list.

02:02.360 --> 02:08.390
And of course I need a context object to be passed from the main activity.

02:08.390 --> 02:16.700
Our data source is passed as an array list and the context object represents the current state and environment

02:16.700 --> 02:18.530
of your application.

02:18.530 --> 02:26.660
It provides access to various resources and services that your app needs to interact with the system

02:26.660 --> 02:28.190
and perform tasks.

02:28.190 --> 02:32.480
And in this case, I needed to create an inflating view.

02:32.480 --> 02:35.210
Now let's create our constructor.

02:35.210 --> 02:38.030
Alt+ Enter Create Constructor.

02:39.140 --> 02:41.570
I need to get these to.

02:42.620 --> 02:44.000
And here we go.

02:44.030 --> 02:46.400
I remove the context.

02:48.500 --> 02:52.210
And here I need to remove this resource.

02:52.220 --> 02:59.810
So the two parameters of the adapter that I need to pass is the array list and the context objects here

02:59.810 --> 03:00.650
in the super.

03:00.650 --> 03:09.350
I need to replace it with our layout that we defined for each item in the grid view grid item layout.

03:09.350 --> 03:13.940
And the third parameter is the shapes array list.

03:13.940 --> 03:18.320
So again, super is referred for the array adapter here.

03:18.320 --> 03:25.910
And if we go to the array adapter class Ctrl and left mouse, you can see array adapter extends base

03:25.910 --> 03:33.140
adapter and if we scroll down to the constructors, we can see I need to pass the context and the layout

03:33.140 --> 03:35.390
resource, which is an N here.

03:35.390 --> 03:40.400
We're using this constructor that we need to pass the context as first parameter.

03:40.430 --> 03:47.180
The second parameter is the layout resource, which is referred for the item grid item layout and the

03:47.280 --> 03:53.310
objects, which is an array list shapes, array list that we passed as parameter.

03:53.340 --> 03:56.760
Then we need to create the view holder.

03:56.790 --> 04:04.710
The view Holder class is used to cache references to the views within an item layout so that they don't

04:04.710 --> 04:08.760
need to be repeatedly looked up during scrolling.

04:08.760 --> 04:16.530
And for our app, I need to create two references, one for the image view and one for the text view.

04:16.530 --> 04:27.330
For that, I'll start by creating the private class private static class text view shape, name, image,

04:27.330 --> 04:34.170
view, shape, alt plus enter to import the image view and the text view classes.
