Android ListView is a ViewGroup which is used to display the list of items in multiple rows and contains an adapter which automatically inserts the items into the list.
The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result. So, it is main source to pull data from strings.xml file which contains all the required strings in Kotlin or xml files.
Adapter holds the data fetched from an array and iterates through each item in data set and generates the respective views for each item of the list. So, we can say it act as an intermediate between the data sources and adapter views such as ListView, Gridview.
Different Types of Adapter –
- ArrayAdapter: It always accepts an Array or List as input. We can store the list items in the strings.xml file also.
- CursorAdapter: It always accepts an instance of cursor as an input means
- SimpleAdapter: It mainly accepts a static data defined in the resources like array or database.
- BaseAdapter: It is a generic implementation for all three adapter types and it can be used in the views according to our requirements.
Now, we going to create an android application named as ListViewInKotlin using an arrayadapter. Open an activity_main.xml file from \res\layout path and write the code like as shown below.
In this file, we declare the LisitView within LinearLayout and set its attributes. Later, we will access the ListView in Kotlin file using the id.
When we have created layout, we need to load the XML layout resource from our activity onCreate() callback method and access the UI element form the XML using findViewById.
We need to run using Android Virtual Device(AVD) to see the output.
- Android Animations in Kotlin
- Kotlin Android Tutorial
- Android Slide Up/Down in Kotlin
- Android EditText in Kotlin
- Android Fade In/Out in Kotlin
- Android LinearLayout in Kotlin
- Android Toast in Kotlin
- Android RelativeLayout in Kotlin
- Android TableLayout in Kotlin
- Android FrameLayout in Kotlin
- Thread Priority in Kotlin & Android
- Android progress notifications in Kotlin
- Difference between Java and Kotlin in Android with Examples
- How to create project in Android Studio using Kotlin
- Why You Should Switch to Kotlin from Java to Develop Android Apps?
- Comparator in Kotlin
- Kotlin Collections
- TimePicker in Kotlin
- Triple in Kotlin
- Chronometer in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.