Android Kotlin / Java-更改可变列表中的值

时间:2019-02-21 00:34:54

标签: java android arrays loops kotlin

我得到了这个可变列表:

[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)]

如何将ilike更改为true的{​​{1}}

这是我尝试过的:

id

预先感谢

5 个答案:

答案 0 :(得分:1)

如果您希望几个项目(可能是1或2?)受到影响,
您可以df = df.replace('', np.nan) df1 = df.groupby('ID', as_index=False).first().fillna('') print (df1) ID Visit11 Visit12 0 1 Orange 1 2 Apple Orange 2 3 Grapes 3 4 Apple 4 5 Not Defined Apple 列表,然后更改CREATE TRIGGER UpdateData ON nfi FOR INSERT AS INSERT INTO SN (esn,ern) SELECT esn,ern FROM inserted where esn not in (select esn from nfi) go 过滤的项目:

filter

答案 1 :(得分:1)

您可以使用find函数查找id = 2的元素并更改其属性:

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

注意:使用question mark if the property is nullable是个好习惯,您不确定它是否为空。

答案 2 :(得分:1)

如果您不想使用setpredicates,请使用iteration替换对象

例如。

val video = (...,read = true) //or however you are getting the current model
val updatedVideo = video
updatedVideo.read = true
vids[vids.indexOf(video)] = updatedVideo

答案 3 :(得分:0)

尝试一下,我假设您的Videos结构是一个类似定义的数据类。 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)
    }
}

答案 4 :(得分:0)

我必须更改几个属性,并且需要保存更改后的对象。因此,以下方法对我来说效果更好:

//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