Display List View in Android

Display List View in Android

A list view is a scrollable list that displays a list of items.

A ListView class is used to create a list view. It is empty and requires an adapter that provides data from source.

app/src/main/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

We create an array that contains a list of items. An ArrayAdapter converts a list of items into View items. The simple_list_item_1 is a built-in XML layout file that contains single TextView.

The setOnItemClickListener method registers a callback. It will be invoked when an item was clicked. We use the getItemAtPosition method to get the item associated with the specified position in the list.

app/src/main/java/com/example/app/MainActivity.java

package com.example.app;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<String> items = Arrays.asList("Yellow", "Green", "Red", "Blue");

        ArrayAdapter<String> adapter = new ArrayAdapter<>(
            this,
            android.R.layout.simple_list_item_1,
            items
        );

        ListView listView = findViewById(R.id.listView);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((parent, view, position, id) -> {
            String item = listView.getItemAtPosition(position).toString();
            Log.d("MY_APP", item);
        });
    }
}

app/src/main/java/com/example/app/MainActivity.kt

package com.example.app

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.ListView

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

        val items: Array<String> = arrayOf("Yellow", "Green", "Red", "Blue")

        val adapter: ArrayAdapter<String> = ArrayAdapter(
            this,
            android.R.layout.simple_list_item_1,
            items
        )

        val listView: ListView = findViewById(R.id.listView)
        listView.adapter = adapter

        listView.setOnItemClickListener { parent, view, position, id ->
            val item: String = listView.getItemAtPosition(position).toString()
            Log.d("MY_APP", item)
        }
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.