Lesson 7: ListView Control (with a list of String) with examples

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

The ListView control, unlike the Spinner that closes after selecting an element, remains visible to several elements (used when displaying a list of similar elements).

In this first example, we will show a list of String (that is to say each element of the list is a String, we will see later that we can have lists of objects of another type: images, icons, several String by element, etc.)

If the list does not enter the space we have set for the ListView allows us to scroll them.

The ListView control is on the “Containers” tab.

Create a project called Project006.

Problem:

Provide a ListView with the names of countries in South America. When selecting a country, show the number of inhabitants of the selected country in a TextView.

The visual interface to implement is as follows: first we have a TextView at the top (whose id we defined with the value tv1, set the textSize property to 30sp (you have to press “view all attributes” to locate this property) and a ListView (we define its id with the value list1):

Lesson 7: ListView Control (with a list of String) with examples - Lesson 7: ListView Control (with a list of String) with examples - Lesson 7: ListView Control (with a list of String) with examples -

Source code:

package com.coding180.project001


import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.*

class MainActivity: AppCompatActivity () {
// coding180.com
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)


        val tv1 = findViewById (R.id.tv1) as TextView
        val list1 = findViewById (R.id.list1) as ListView
        val countries = arrayOf ("Argentina", "Chile", "Paraguay", "Bolivia", "Peru", "Ecuador", "Brazil", "Colombia", "Venezuela")
        var inhabitants = arrayOf(40_000_000, 17_000_000, 6_500_000, 10_000_000, 30_000_000, 14_000_000, 183_000_000, 44_000_000, 31_000_000, 3_500_000)

        var adapter1 = ArrayAdapter <String> (this, android.R.layout.simple_list_item_1, countries)
        list1.adapter = adapter1
        list1.setOnItemClickListener {adapterView, view, i, l ->
            tv1.text = "Population of: ${inhabitants[i]}"
        }
    }
}

We get the reference of the TextView and the ListView:

        val tv1 = findViewById (R.id.tv1) as TextView
        val list1 = findViewById (R.id.list1) as ListView

We define two parallel arrangements where we store the names of countries in one and store the number of inhabitants of those countries in the other:

        val countries = arrayOf ("Argentina", "Chile", "Paraguay", "Bolivia", "Peru", "Ecuador", "Brazil", "Colombia", "Venezuela"
        var inhabitants = arrayOf (40_000_000, 17_000_000, 6_500_000, 10_000_000, 30_000_000, 14_000_000, 183_000_000, 44_000_000, 31_000_000, 3_500_000)

In Kotlin for an easy reading of large integers we can have the underscore character (then when compiling does not take into account this character)

We create an object of class ArrayAdapter in a similar way as we did when we saw the class Spinner and we linked it with the ListView:

        var adapter1 = ArrayAdapter <String> (this, android.R.layout.simple_list_item_1, countries)
        list1.adapter = adapter1

We call the setOnItemClicListener method of the ListView class and pass it as a parameter a lambda that will be executed each time the user selects an item from the ListView:

        list1.setOnItemClickListener {adapterView, view, i, l ->
            tv1.text = "Population of: $ {inhabitants [i]}"
        }

This lambda the third parameter indicates the position of the selected item in the ListView. With this value we can locate the respective value in the settlement of inhabitants.

To encode the lambda we can use the facility provided by Android Studio to generate it automatically, for this we type:
Lesson 7: ListView Control (with a list of String) with examples - Lesson 7: ListView Control (with a list of String) with examples - Lesson 7: ListView Control (with a list of String) with examples -

Then press the “Enter” key and Android Studio automatically generates the lambda.

Within the lambda, we modify the content of the TextView with the name of the country and the number of inhabitants of that country. The lambda receives the position of the selected item in the ListView in the third parameter.

When we run the project we can see an interface in the emulator similar to this:

Lesson 7: ListView Control (with a list of String) with examples - Lesson 7: ListView Control (with a list of String) with examples - Lesson 7: ListView Control (with a list of String) with examples -

About The Author

Related posts

Leave a Reply