Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
}
like image 561
Eduard Unruh Avatar asked Feb 21 '19 00:02

Eduard Unruh


People also ask

How do you edit a list on Kotlin?

First, we create a mutable list using the mutableListOf built-in method, and we obtain the iterator by executing the listIterator() method. Then, we simply verify if the element is even. If it is, we use the set method to modify the last element retrieved.

How do you add data to a mutable list in Kotlin?

To add an element to a Mutable List in Kotlin, we can use add(element), or add(index, element) functions. add(element) adds element to the end of this Mutable List. add(index, element) adds element to this Mutable List at the given index.

How do you replace items in mutable list Kotlin?

We can modify the contents of a List only if the list is mutable. To replace an element in a Mutable List at given index in Kotlin, call set() function on this list object and pass the index and the element as arguments.


5 Answers

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.

like image 102
Sergey Avatar answered Oct 13 '22 22:10

Sergey


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 }
like image 33
forpas Avatar answered Oct 13 '22 21:10

forpas


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)
    }
}
like image 12
Rafa Avatar answered Oct 13 '22 21:10

Rafa


I had to change several properties and I had a need to hold the changed object. Therefore following approach worked better for me:

//First, find the position of the video in the list
val videoPosition= list.indexOfFirst {
   it.id == 2
}

//Now get your video by position and make changes
val updatedVideo = list[videoPosition].apply {
   //Make all changes you need here
   ilike = true

   //...
}

//Finally, replace updated video into your list. 
list[videoPosition] = updatedVideo 
like image 7
Azizjon Kholmatov Avatar answered Oct 13 '22 23:10

Azizjon Kholmatov


Use set to replace the object if you don't want to use predicates or iteration

Eg.

val video = (...,read = true) //or however you are getting the current model
val updatedVideo = video
updatedVideo.read = true
vids[vids.indexOf(video)] = updatedVideo
like image 4
Aziz Avatar answered Oct 13 '22 21:10

Aziz