‫Hello my friends, and welcome back.
‫In this video we're going to learn about adapters in Android.
‫An adapter is a bridge between the user interface components and the data source.
‫Adapters are commonly used to populate data into views such as filling a list, view, recycler, view,
‫or spinner with data from a data source like an array or a database.
‫The primary purpose of an array is to inflate a views, converting each item in the data source into
‫a view that can be displayed in the user interface, and binding data, which populates the inflated
‫views with the corresponding data from the data source.
‫Let's jump into Android Studio and learn how to create adapters and how to use them.
‫I created this empty project called Adapters Application and by the way, you can download the source
‫codes from the resources folder under each section here.
‫Inside this layout, I'll create a list view uh element and Matchparent Matchparent and give it an ID
‫list view.
‫Click on it, infer the constraints and here we go.
‫A list view is a view group that displays a scrollable list of items.
‫Each item in the list is typically a view that the list view displays.
‫It's one of the fundamental UI components used to present data to users in a scrollable, vertically
‫oriented list.
‫Let's jump into main activity and start initializing list view here.
‫Inside the Oncreate method, I'll start with val my list view.
‫It's of type list view equals.
‫To find the view by id r.id dot list view.
‫The second step in creating a list view is to create a data source.
‫The data source is the underlying set of data that the list view displays.
‫As the data source is typically an array, a list, or some other collection of items, the adapter
‫is responsible for converting items from the data source into views that can be displayed in the list
‫view.
‫For that, I create val operating systems, which is an array of strings, and here I'll start with
‫the items.
‫These are the four elements of the operating systems array.
‫Let's move to the adapter.
‫The list view doesn't directly hold the data it displays.
‫Instead, it relies on the adapter to populate the data.
‫The adapter is responsible for creating views for each item in the data source, and binding data to
‫those views.
‫Common adapters for list view include array adapter, based adapter, and custom adapters.
‫Since our data source is an array, I need to use array adapter.
‫So for that val my adapter equals to array adapter.
‫And here I need to pass three parameters.
‫The first one is the context.
‫The second one is the layout android or layout dot simple list item one.
‫And the third parameter is the list and the array that we are going to populate, which is operating
‫systems.
‫Let's dive into this code.
‫Array adapter is a type of adapter provided by Android for use with arrays or lists of data.
‫And here I'm passing three parameters.
‫The first one is the context.
‫The first parameter is the context which represents the current state of the application.
‫In this case, it assumes to be an activity or another context aware component.
‫You might use this if you are inside an activity.
‫And here we are inside the main activity.
‫So always use this as a context if you are in the main activity.
‫Android dot r dot layout dot simple list item.
‫The second parameter is the layout resource id that defines how each item in the list should be displayed.
‫This layout is a built in layout provided by Android.
‫It contains a single text view, and it's often used for simple lists of strings.
‫If you want to display this layout, Ctrl and left mouse and here you will go to the layout and only
‫contains this text view.
‫Operating systems.
‫The third parameter is the data source, which is the array or list of items that you want to display
‫in the list view.
‫So whenever you need to, you need to create an array adapter.
‫You need to pass context as first parameter template layout that will act as a template for every item
‫displayed in the list view.
‫And the third parameter is the data source.
‫In the next videos, we're going to use our custom layouts to display the items of a list view.
‫The last step is to set the adapter to the list view.
‫It's very simple.
‫List view dot adapter equals to my adapter.
‫I'm linking this list view with my adapter.
‫Save the project and run and congratulations guys!
‫We created this list view.
‫You can see the items are populated in a template layout that we used, and it's a built in layout called
‫Simple List Item.
‫Also, we use the adapter to inflate all the elements into this view to this list of view.