I am trying to update only one item but the below code is updating all
logValue("changes before : $availabilityMasterList")
availabilityMasterList.filter { it.days.lowercase() == day.lowercase() }.forEach {
it.shifts.add(Shift(shiftname, "", ""))
}
logValue("changes broadcast : $availabilityMasterList")
log output before and after
changes before : [MasterAvailableListData(days=Monday, drsAvailabilityDate=, id=0, shifts=[], edit=false), MasterAvailableListData(days=Tuesday, drsAvailabilityDate=, id=1, shifts=[], edit=false), MasterAvailableListData(days=Wednesday, drsAvailabilityDate=, id=2, shifts=[], edit=false), MasterAvailableListData(days=Thursday, drsAvailabilityDate=, id=3, shifts=[], edit=false), MasterAvailableListData(days=Friday, drsAvailabilityDate=, id=4, shifts=[], edit=false), MasterAvailableListData(days=Saturday, drsAvailabilityDate=, id=5, shifts=[], edit=false), MasterAvailableListData(days=Sunday, drsAvailabilityDate=, id=6, shifts=[], edit=false)]
---------- below output after above query --------------------
2021-11-23 14:50:04.428 30467-30467/com.ohc.admin D/OhcDocHosp: ---------- changes broadcast : [MasterAvailableListData(days=Monday, drsAvailabilityDate=, id=0, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Tuesday, drsAvailabilityDate=, id=1, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Wednesday, drsAvailabilityDate=, id=2, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Thursday, drsAvailabilityDate=, id=3, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Friday, drsAvailabilityDate=, id=4, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Saturday, drsAvailabilityDate=, id=5, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Sunday, drsAvailabilityDate=, id=6, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false)]
CodePudding user response:
I highly suspect that each of the MasterAvailableListData items have the exact same reference to the same empty shifts list. So if you add a shift to a shifts list on any of the items it looks like you added it to all, because it is in fact the same list. Make sure you create the MasterAvailableListData items each with their own initial empty shifts list
CodePudding user response:
It weirds that it returns only one element and that the whole list is updated...
If you need to update one item only, use the firstOrNull
Do something like that
availabilityMasterList.firstOrNull { it.days.equals(day, true) }.?apply {
shifts.add(Shift(shiftname, "", ""))
}
firstOrNull will returns that matched value if any, otherwise null
You don't need to iterate on the result, because you will obtain the item and not a list.
?.apply
It will prevent from the null pointer and update the item only if the item exists in the list. apply will also return the element, and you don't need to use the it or the this to reference the object.
BTW, if you need a case-insensitive comparison, you can use the equals function with the ignoreCase param. (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/equals.html)
"example".equals("EXAMPLE", ignoreCase = true)