# How to make a simple selectable list

> Display a list of texts and capture the selection with ListView (SINGLE or MULTIPLE).

Source: https://crossfly.dev/docs/cookbook/lists/lista-simples — CrossFly documentation. Full docs as one file: https://crossfly.dev/llms-full.txt

# How to make a simple selectable list

**Purpose.** Show a scrollable list of text items and react when the user
selects one (SINGLE) or several (MULTIPLE). The `ListView` is an embeddable `Component`: you
add it to any layout, with no window needed.

## APIs used
- `new ListView(String[] items, ListView.ItemType type)` — creates the list from an array of texts.
- `new ListView(String title, String[] items, ListView.ItemType type)` — same, with a title header.
- `ListView.ItemType.SINGLE` / `ListView.ItemType.MULTIPLE` — the only two enum values. `SINGLE` = tap selects and fires `onConfirm`; `MULTIPLE` = checkboxes + "Confirm" button.
- `ListView.onConfirm(Component.Action action)` — callback fired on confirm. Receives the `ListView` itself as the event source.
- `ListView.getSelectedText()` / `getSelectedIndex()` — first selected item (text/index), or `null`/`-1`.
- `ListView.getSelectedTexts()` / `getSelectedIndices()` — all selected items (useful in MULTIPLE).
- `ListView.confirmText(String)` — text of the confirmation button (default `"Confirmar"`, MULTIPLE only).

## Complete example (copy, paste and run)
```java
package com.exemplo;

import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;

/** Runnable main screen. Point the launcher (classname) and run: gradlew :lwjgl3:run */
public class MainWindow extends Window
{
   @Override
   public void onShow() throws Exception
   {
      super.onShow();
      setLayout(new VerticalLayout());

      String[] frutas = {"Maçã", "Banana", "Laranja", "Uva"};

      // SINGLE: the tap already selects and fires onConfirm
      ListView lista = new ListView("Escolha uma fruta", frutas, ListView.ItemType.SINGLE);
      lista.onConfirm(e ->
      {
         String texto = lista.getSelectedText();   // e.g.: "Banana"
         int idx = lista.getSelectedIndex();       // e.g.: 1
         Debug.log("Selecionado: " + texto + " (#" + idx + ")");
         return null;
      });
      add(LAY_FILL, lista);   // ListView is a Component: goes straight into the layout
   }
}
```

> The snippet below shows **only the core**. To run it, place it inside the `MainWindow` skeleton above.

## Minimal snippet
```java
// verified in source: ListView.java
String[] frutas = {"Maçã", "Banana", "Laranja", "Uva"};

// SINGLE: the tap already selects and fires onConfirm
ListView lista = new ListView("Escolha uma fruta", frutas, ListView.ItemType.SINGLE);
lista.onConfirm(e ->
{
   String texto = lista.getSelectedText();   // e.g.: "Banana"
   int idx = lista.getSelectedIndex();       // e.g.: 1
   Debug.log("Selecionado: " + texto + " (#" + idx + ")");
   return null;
});
layout.add(.9f, lista);   // ListView is a Component: goes straight into the layout
```

```java
// MULTIPLE: checkboxes; onConfirm fires only when tapping the "Confirm" button
ListView multi = new ListView("Selecione ingredientes", frutas, ListView.ItemType.MULTIPLE);
multi.confirmText("Adicionar");
multi.onConfirm(e ->
{
   String[] escolhidas = multi.getSelectedTexts();   // all the checked ones
   return null;
});
layout.add(.9f, multi);
```

## Go deeper
- Need a modal popup instead of embedded? See [popup-de-selecao.md](/docs/cookbook/lists/popup-de-selecao).
- Thousands of items? Don't use `ListView` (it materializes them all); see [lista-virtual-grande.md](/docs/cookbook/lists/lista-virtual-grande).
- Widget guide: [ListView](/docs/ui/widgets/listview) · Canonical source: Chapter 15
