Kotlin ListView example | Android

In this Kotlin tutorial, we’re gonna look at way to show list of items with Android ListView.

Related Post: Kotlin GridView example: Show List of Items on Grid | Android

More Practice: Kotlin SQLite example – CRUD operations with ListView | Android

I. Technologies

– Android Studio 3
– Kotlin 1.1.51

II. Overview

1. Goal

We will build an Android App that uses ListView to show list of items:


2. Android ListView
2.1 Input Types

Input (items) of a list can be arbitrary Kotlin object. We use an adapter to extract data from this Kotlin object, then assign object fields’value to the views in each row of ListView.

For example:

Class for Kotlin object is called data model.

2.2 Adapter

Adapter (extends the BaseAdapter class) manages the data model and adapts it:

2.3 Listener

We can also set Listener for reacting to selections in the list. For example, handle onItemClick event:

2.4 ViewHolder

ViewHolder allows to avoid the findViewById() method in adapter. It includes references to views in item layout.

BaseAdapter has getView() method that provide a convertView object, we can get the instance of the ViewHolder via the convertView.tag:

3. Project Structure


III. Practice

1. Set up Project

– Create New Project:

– Add images (found in sourcecode) to drawable.

2. Layout
2.1 Main Activity

Open res/layout/activity_main.xml file, add ListView:

2.2 Item Layout

Add note.xml layout file to res/layout:

3. Data Model

Add Kotlin class:

4. Activity

We will:
– create ViewHolder class
– create BaseAdapter subclass
– set adapter for ListView
– add Listener to ListView

IV. Source Code


By grokonez | December 6, 2017.

Last updated on July 13, 2018.

Related Posts

11 thoughts on “Kotlin ListView example | Android”

  1. I cannot explain why, but

    has to be changed to

    to get this thing running. Seems to be a change in the target API issue.
    I just adaptet some informations from here: https://stackoverflow.com/questions/45267041/not-enough-information-to-infer-parameter-t-with-kotlin-and-android

  2. sorry, has to be

  3. Hey JSA, this is a very useful tutorial. Exactly what I was looking for!
    I’m a novice programmer who’s very interested in Android Development (with Kotlin) but I find this a bit difficult to understand. I got your code to work with some extra work, but I am not 100% sure what every line does or why certain things are added in. Is there a possibility you can place a comment behind every line of newly introduced code explaining why you added that code?

    I feel like this would be incredibly useful to starting programmers, it’s one of the best ways to teach in my opinion. Looking forward to your response 🙂

    1. No, it sucks. Google is going to remove ListView and implemented RecycleView. and hundreds of elements they change in next coming every update. Not a good choice to work on Android development anymore

  4. Hello,

    nice turorial 🙂 But I´m getting an error, when I start the activity -> java.lang.RuntimeException: Unable to start activity ComponentInfo{de.company.listviews/de.company.listviews.ListView2}: java.lang.IllegalStateException: lvNotes must not be null

    Any ideas? I can´t find the problem. Why should my lvNotes be null? I also copied and pasted the code, to be sure it is the same. My target sdk version is 27, so I tryied both “init´s” in the ViewHolder.

    Best Regards

Got Something To Say:

Your email address will not be published. Required fields are marked *