Kotlin Android Custom ListView
In the previous tutorial Kotlin Android ListView, we have created an example of default ListView. Android provides a facility to customize the ListView. In this tutorial, we will customize our ListView.
An Adapter class is used to add the list items in the list. It bridges the list of data between an AdapterView with other Views components (ListView, ScrollView, etc.).
Kotlin Android Custom ListView Example
In this example, we will create a custom ListView and perform click action on list items. In this custom ListView, we add one image and two different text descriptions for each row of ListView.
In the activity_main.xml file add a ListView component to display the list of items.
Create a layout file named as custom_list.xml in the layout directory and add one ImageView and two TextView. The ImageView is used to display image, one TextView to display the title and another TextView to display text description.
Add the following code in the MainActivity.kt class. In this class, we are creating two String array and one Int array to store string text and image id respectively.
From this class, we are calling a custom adapter class named as MyListAdapter by passing the context and data as the parameters. The listView.adapter = myListAdapter set the return adapter to the ListView.
Now, create a custom adapter class named as MyListAdapter.kt which populates the data model into the ListView.