How to use ListView in an Android app?

ListView is a vital component in Android development that allows you to display a list of scrollable items efficiently. It makes it easy to manage and display a large set of data on the screen. Below is a simple example of how to implement a ListView in an Android app. This example includes an ArrayAdapter to bind the data to the ListView.

// MainActivity.java package com.example.listviewexample; 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 = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items); listView.setAdapter(adapter); } }

This example demonstrates how to create a simple ListView with predefined items. Make sure to define the corresponding layout in XML and grant the necessary permissions in your AndroidManifest.xml if needed.


ListView Android ListView example Android UI components ListView in Android