Reputation: 2365
I can use Code A add all id of MVoice
in a List
to val selectedIDs: MutableSet<Int>
.
I think I can simplify it, and add all id of List at one time, but Code B doesn't work, how can I fix it?
Code A
val selectedIDs: MutableSet<Int> = mutableSetOf()
val listVoiceBySort: LiveData<List<MVoice>> =_listVoiceBySort
listVoiceBySort.value?.forEach(){
selectedIDs.add(it.id)
}
Code B
val selectedIDs: MutableSet<Int> = mutableSetOf()
val listVoiceBySort: LiveData<List<MVoice>> =_listVoiceBySort
listVoiceBySort.value?.let{
selectedIDs.addAll(it.id)
}
Upvotes: 0
Views: 62
Reputation: 1170
This should work:
val selectedIDs: MutableSet<Int> = mutableSetOf()
val listVoiceBySort: LiveData<List<MVoice>> =_listVoiceBySort
listVoiceBySort.value?.map { it.id }?.let {
selectedIDs.addAll(it)
}
Upvotes: 1