‫Let's continue with the Getview method.
‫Here I'll create a new variable.
‫It's of type view.
‫And let me check for the converted view.
‫If it is equal to null, we need to inflate a new view.
‫For that view equals to inflator dot inflate method r dot layout dot.
‫And here I need to specify my custom layout.
‫The second parameter is parent and the third parameter is false.
‫Let us break down this code.
‫I'm checking if the converted view which is the parameter here.
‫If it is equal to null, we need to inflate a new view using layout inflator.
‫This is the view that will be displayed for the current item.
‫The layout inflator class is used to instantiate layout XML file into its corresponding view objects,
‫which can then be used in the user interface.
‫And this inflator is the instance of the layout inflator we created before, which is responsible for
‫inflating layouts.
‫The first parameter is R dot layout dot my custom layout.
‫This is the identifier for the layout resource that you want to inflate, and in this case, I'm going
‫to inflate my custom layout that refers to an XML layout defined in the rest slash layout directory.
‫The parent.
‫This is the parent view group to which the inflated view will be attached.
‫It's used in the layout inflation process to determine the layout parameters.
‫In this case of list view, parent is the list view itself and it's passed as a parameter in the get
‫view method.
‫False.
‫This parameter specifies whether the inflated view should be attached to the parent during the inflation
‫process.
‫Setting it to false means that the inflated view is not automatically attached to the parent immediately.
‫Instead, you will manually attach it later.
‫So in summary, the line here is using the layout inflator to take the Xaml layout defined in the my
‫Custom Layout.xml file, inflate it into a view object and return that view.
‫This view will represent an item in the list and it's not immediately attached to the list view.
‫Attaching the view is typically done later in the getter view method, and setting it to false allows
‫for more control over when and how the view is attached to the parent.
‫Let's continue.
‫Else if the converted view is not null, then reuse it.
‫And in this case I am setting the view equals to the convert view.
‫Reusing the views is a performance optimization to reduce the overhead of inflating new views repeatedly
‫as the user scrolls through the list.
‫The second step is to bind data to the view.
‫Here I'll start val item equals to get item position, but to prevent any possible error you can use
‫it and use a string because get item will return any object.
‫And I'm casting this object as a string.
‫Then I need to use val title text view equals to view dot find view by id it's of type text view are
‫id dot text view custom.
‫I am initializing this text view here in the get view method, so there is no need to initialize this
‫text in the Oncreate method of the main activity.
‫We need to initialize this custom text view in this custom layout inside the get view method.
‫Again guys, whenever you have any views, you need to initialize them in the get view method, I need
‫to set this title.
‫The text view dot text equals to item dot toString and you can get rid of toString because I'm casting
‫it as a string here so you can remove it.
‫Now we need to return the view, because the returning type of the get view method is a view.
‫We need to return this variable view that we created.
‫So inside the bind data tab, regardless of whether a new view is inflated or an existing one is reused,
‫the method then binds the data.
‫The from the data set to the views within the item layout.
‫This involves finding the relevant views, for example, this text view by their IDs and setting their
‫text or properties based on that data at the specified position.
‫The final step is to return the populated view which will be displayed in the adapter view.
‫Now let's move to the main activity and here I need to remove this array adapter.
‫I commend it and here I need to create my custom adapter.
‫So custom adapter it's of type.
‫Custom adapter equals to custom adapter.
‫My custom adapter.
‫And when you specify the constructor there is no need to use this.
‫Okay, my custom adapter.
‫And here I need to pass the two parameters.
‫The context of the main activity and the list, which in this case is the operating systems.
‫If we look closer, we have type mismatch required.
‫List of string found array of string.
‫Since I'm dealing with custom adapters, I prefer using lists instead of arrays.
‫So I'll change this array to list of and.
‫This will solve the problem.
‫And make sure you always use lists with custom adapters.
‫It's better and more dynamic, and here I'll change the adapter to custom adapter.
‫Don't miss this very important step to create and initialize your custom adapter and set it to your
‫list.
‫Save the project and run.
‫And here we go!
‫This is our custom list.
‫All items are applying the template.
‫Read text views and bigger text views displayed in this list view.
‫This is how we use custom adapters in Android.