Visit Sponsor

Written by 4:42 pm Android

Android ListView Example (with ArrayAdapter)

Overview

ListView is a UI component in Android that displays a vertically scrollable list of items. ListView works with an Adapter that binds data from a data source such as an array or list to views displayed in the list. Using an adapter simplifies displaying large sets of data without manually creating view items for each entry.

In this tutorial, you will learn how to implement a simple ListView that displays text items using ArrayAdapter in Android with Java.

What Is ListView?

ListView is a subclass of AdapterView that presents items in a vertical, scrollable list. It does not know by itself how to render each list item — it requests item views from an Adapter as needed.

To display items in a ListView:

  1. Define the ListView in your layout XML.
  2. Prepare a dataset (such as a String array or a list).
  3. Use an Adapter (for example, ArrayAdapter) to bind data to the ListView.
  4. Call setAdapter() on the ListView to attach the adapter.

Step 1: Define the Layout XML

In your layout resource (e.g., res/layout/activity_main.xml), include a ListView element. You can adjust android:layout_width and android:layout_height as needed.

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

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

</LinearLayout>

This defines a ListView that fills the available space in the screen.

Step 2: Prepare the Data Source

In your activity class, declare an array of items to display. These items can be Strings or custom objects.

String[] items = {
    "Android",
    "Java",
    "Kotlin",
    "RecyclerView",
    "ListView",
    "Adapters",
    "Fragments",
    "Services"
};

Step 3: Bind Data with ArrayAdapter

Inside your onCreate() method, connect the ListView with an ArrayAdapter:

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        String[] items = {
            "Android",
            "Java",
            "Kotlin",
            "RecyclerView",
            "ListView",
            "Adapters",
            "Fragments",
            "Services"
        };

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

        listView.setAdapter(adapter);
    }
}

Here:

  • android.R.layout.simple_list_item_1 is a built-in layout that displays a single text item per row.

As the ListView displays items that scroll vertically by default, it automatically manages recycling views for performance.

Step 4: Respond to Item Clicks (Optional)

To handle user interactions such as tapping an item, set an OnItemClickListener:

listView.setOnItemClickListener((parent, view, position, id) -> {
    String selected = (String) parent.getItemAtPosition(position);
    Toast.makeText(MainActivity.this, selected, Toast.LENGTH_SHORT).show();
});

This retrieves the clicked item and displays it as a toast message.

When to Use ListView

ListView is suitable for simple lists of text or small datasets. However, for more complex or large lists — especially with dynamic layouts / performance requirements — Android’s RecyclerView is the recommended modern component.

Summary of Core Concepts

  • ListView displays vertically scrolling lists of data.
  • ArrayAdapter bridges between a data array and the ListView UI.
  • Use setAdapter() on the ListView to attach the adapter.
  • Handle user interactions using listeners such as setOnItemClickListener.
Visited 7 times, 1 visit(s) today
Close