Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda