Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
Change a value in mutable list in Kotlin

I got this mutablelist:

[Videos(id=4, yt_id=yRPUkDjwr1A, title=test4, likes=0, kat=pranks, ilike=false), Videos(id=3, yt_id=WkyUU9ZDUto, title=test3, likes=0, kat=pranks, ilike=false), Videos(id=2, yt_id=B_X9OQqtduE, title=test2, likes=0, kat=animals, ilike=false), Videos(id=1, yt_id=ywaKlGNiv80, title=test1, likes=0, kat=animals, ilike=false)]

How can I change ilike to true where id is 2

This is what I've tried:

for (i in 0 until vids!!.size) {
    Log.d("lets", vids!!.get(i).title)
        
    if(vids!!.get(i).id == 2){
        vids!!.get(i).ilike = true
    }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can use find function to find the element with id = 2 and change its property:

vids?.find { it.id == 2 }?.iLike = true

Note: it is a good practice to use question mark if the property is nullable and you unsure whether it is null or not.

over 4 years ago · Santiago Trujillo Relatório

0

If you expect few items (maybe 1 or 2?) to be affected,
you can filter the list and then change iLike of the filtered items:

vids!!.filter { it.id == 2 }.forEach { it.iLike = true }
over 4 years ago · Santiago Trujillo Relatório

0

Try this, I'm assuming your Videos structure is a data class defined somewhat like so. data class Videos(val id: Int, val yt_id: String, val title: String, val likes: Int, val kat: String, val ilike: Boolean)

list.forEachIndexed { index, video ->
    video.takeIf { it.id == 2}?.let {
        list[index] = it.copy(ilike = true)
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda