I'm adding new items to mutable list by setting header and detail data in val production
.
val productionList = mutableListOf<ProductionData>()
val production = ProductionData()
//Assign header data
production.dateHeader = headerData.date
production.timeHeader = headerData.time
//Iterate detail data
for (item in detailData) {
//Assign detail data
production.timeDetail = item.timeDetail
//Add new product
productionList.add(production)
}
The problem is that on second iteration, production.timeDetail
is modified but the productionList[0]
is also modified.
How can I add items by value and not by reference as it is now? What am I doing wrong?