openlang

Showing positions from a listOf

I'm working through this video tutorial (https://www.youtube.com/watch?v=saKrGCWlJDs).

class DirectoryActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        val contacts =
            listOf("Ellis", "Bran", "Ivo", "Miro", "Taren", "Keir", "Neris", "Aven", "Doran", "Fenn", "Kelan", "Orin", "Riven", "Soren", "Tolan")

        setContent {
            FernbankTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { insets ->
                    Welcome(
                        label = "Android",
                        modifier = Modifier.padding(insets)
                    )

                    LazyColumn {
                        items(contacts) {
                            ContactRow(label = it)
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun Welcome(label: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $label!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun WelcomePreview() {
    FernbankTheme {
        Welcome("Android")
    }
}

@Composable
fun ContactRow(label: String) {    
    Card(modifier = Modifier.fillMaxSize().padding(14.dp)) {
        Text(text = label, modifier = Modifier.padding(28.dp))
    }
}

Here's what I'd like to achieve:

Within ContactRow, I'd like to show (print) each position in the “contacts” list as well.

For instance:

0 Ellis

1 Bran

2 Ivo

My understanding of lists (arrays) is that “Ellis” has index 0, “Keir” has index 5 and “Orin” has index 11.

@Composable
fun ContactRow(label: String) {
    //show the position for "label" to the left of "Text"
    Card(modifier = Modifier.fillMaxSize().padding(14.dp)) {
        Text(text = label, modifier = Modifier.padding(28.dp))
    }
}

The conversation continues 2

Follow the reasoning
  1. Permalink

    Is this what you were looking for?

    items(contacts.withIndex()) { (position, entry) ->
        ContactRow("$position $entry")
    }

Other sides of the same question

  1. Making the sender appear as a label instead of an email address using Java Mail

    Im sending messages through the Java Mail API. On arrival, the inbox displays the address that sent the message. Id like some…

    Replies 2Latest activity java
  2. Dynamics CRM access from TypeScript

    Because my CRM code runs in a webresource, I have to write to retrieve the entity's data. Regrettably, that expression fails…

    Replies 2Latest activity typescript
  3. What date formatting does a Gleam HTTP response header need?

    I want to send a Date header. The required format looks like this: This is my current attempt:

    Replies 3Latest activity gleam