Android Listview in kotlin with example


Introduction

The ListView in Android has the adapterView attributes. This view will display a vertical list of scrollable views that are layered one on top of the other. With the help of an adapter, items from an array or database are imported into the list. The setAdaptor() function is used to display the list's contents and to combine a list and an adapter together to make a list.

The ViewGroup is the component of ListView that is used to display the list of objects in an android application. It provides an adapter that allows for the dynamic addition of items to the list. The primary responsibility of the adapter is to get data from a database or array and add each item to the list. The most important source of data is the strings.xml file, which contains all of the necessary strings in files written in Java or XML.

Android Listview

A view group a component of ListView displays items in a way similar to a list and has the capability to scroll vertically. Android applications typically make use of the listview component.

A simple illustration of a ListView is, a contact application on our phone that presents a list of contacts in the format of a ListView which is scrollable.

Adapter in Android

The information that is obtained from the array is stored in the adapter, which then iterates over every element of the data set and creates the relevant views for each and every item in the list. In this manner, it functions as a bridge linking the various data sources and adapter views, such as ListView and GridView.

Types of Adapter

  • ArrayAdapter − An input may either be a list or an array, but it always accepts the latter. Additionally, we are able to save list components using the strings.xml file.

  • CursorAdapter − It never restricts the input to anything other than a cursor instance.

  • SimpleAdapter − Its primary function is to manage data that is not dynamic and is instead stored in resources such as databases or arrays.

  • BaseAdapter − It is an universal implementation that works across all three different kinds of adapters and may be applied to the views whenever it is required to do so.

Algorithm

  • Step 1 − Open the XML file that is present in the path\re\layout

  • Step 2 − Paste the code given above in the XML file

  • Step 3 − There is a java folder from there select the MainActivity.java file and paste the above code.

  • Step 4 − Choose an emulator or connect a device on which you want to run your application

  • Step 5 − Deploy the code by clicking on the run button that is on the top of the screen.

  • Step 6 − The application will be automatically opened on the device or an emulator where you can see ListView containing the names of sports.

Let’s look at an example of ListView in Kotlin −

In this case, the first thing that needs to be done is to open the xml file that has been given the name activity main.xml and can be found in the res/layout folder.

activity_main.xml-

We define and configure the ListView in this file, which is located in the LinearLayout section. In the future, using the id, we will extract the ListView that is included inside the Kotlin code. We define and configure the ListView in this file, which is located in the LinearLayout section. In the future, using the id, we will extract the ListView that is included inside the Kotlin code.

Here, we've loaded the activity main.xml file by using the onCreate () callback method. Then, we used findViewById to retrieve the file's contents.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">
   <ListView
      android:id="@+id/sportslist"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
   </ListView>
</LinearLayout>

MainActivity.kt-

package com.example.myapplication
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import android.view.Menu
import android.view.MenuItem
import com.example.myapplication.databinding.ActivityMainBinding
import android.widget.ArrayAdapter
import android.widget.ListView
class MainActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)

      // use arrayadapter and define an array
      val arrayAdapter: ArrayAdapter<*>
      val users = arrayOf(
         "Football", "Cricket", "Basketball",
         "Hockey", "Golf","Tennis","Baseball"
      )
      // access the listView from xml file
      var mListView = findViewById <ListView>(R.id.sportslist)
      arrayAdapter = ArrayAdapter(this,
         android.R.layout.simple_list_item_1, users)
      mListView.adapter = arrayAdapter
   }
}

Output

Here we have used Android virtual device(AVD) or emulator to run the application. You can rum this application on your device also.

Conclusion

We have gained an understanding of how to use Kotlin to construct ListView in Android. You can begin with the algorithm and then go to the code portion of the document. We have also been familiar with the process of displaying the items of an array by using a ListView. After that, we selected an emulator to use for running the program, and then we deployed the code.

Updated on: 23-Aug-2023

661 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements