1

I am new to android development.

I want to concatenate (Add a string to each item in an array list) and set it to my spinner

I have 2 arrays:

Array 1 = { Item1, Item2, Item3, Item4 }

Array 2 = { A, B, C, D }

So I am setting only Array1 in spinner like this:

ArrayAdapter(requireContext(), R.layout.spn_item_selected, Array1)

Right now, this is what is displayed in the spinner:

Item1

Item2

Item3

Item4

How do I add items in array 2 to the items in array 1 so I can display something like this in the spinner

Item1, A

Item2, B

Item3, C

Item4, D

For a string I could do this:

 val a = "Hello"
 val b = "Baeldung"
 val c = a + " " + b
 //result
 Hello Baeldung

But this is an array all gotten from a backend

Sorry if this may be simple, I have searched but didnt find what I'm looking for.

Ibccx
  • 105
  • 3
  • 11

1 Answers1

0

You can use zip with transform function to combine both arrays to one:

val l1 = listOf("item1", "item2", "item3")
val l2 = listOf("a", "b", "c")


val res = l1.zip(l2) { a: String, b: String -> "$a,$b" }

//[item1,a, item2,b, item3,c]
Ben Shmuel
  • 1,819
  • 1
  • 11
  • 20