我有一个包含可变列表的对象
object TrackingEventsList {
var EventDate: String = ""
var EventDescription: String = ""
}
object Waybill {
var WaybillNumber: String = ""
var OriginHub: String = ""
var TrackingEvents: MutableList<TrackingEventsList> = ArrayList()
}
当我尝试添加Waybill.TrackingEvents时,所有以前的实例将被覆盖并复制最后添加的TrackingEvent。
private fun fillTracking(events: NodeList) {
var list = TrackingEventsList
for (x: Int in 0 until events.length) {
var tName = (events.item(x) as Element).tagName
var event = (events.item(x).firstChild as Text).wholeText
if (tName == "EventDate") {
list.EventDate = event
}
if (tName == "EventDescription") {
list.EventDescription = event
}
}
Waybill.TrackingEvents.plus(list)
}
调用fillTracking 3次后的结果:
Waybill.TrackingEvents[0].EventDescription = "Event3"
Waybill.TrackingEvents[1].EventDescription = "Event3"
Waybill.TrackingEvents[2].EventDescription = "Event3"
答案 0 :(得分:8)
object
是单例,这意味着您无法对其进行初始化,并且它在全局范围内只有一个实例。因此,当您更改单个实例的项目时,您将覆盖之前的数据。
您应该将它们(或至少TrackingEventsList)都更改为class
。如果Waybill
的变量是实例敏感的,则它也必须是一个类。但是在您添加的代码中,找不到任何可以将您用作单例的东西,因此我将其保留为一个。
class TrackingEventsList (
var eventDate: String = "",
var eventDescription: String = "")
/**
* Also want to point out that this is still a singleton. If the data inside is instance-specific, you need to change it
* to a class.
*/
object Waybill {
var waybillNumber: String = ""
var originHum: String = ""
var trackingEvents: MutableList<TrackingEventsList> = ArrayList()
}
private fun fillTracking(events: NodeList) {
val item = TrackingEventsList()
for (x: Int in 0 until events.length) {
var tName = (events.item(x) as Element).tagName
var event = (events.item(x).firstChild as Text).wholeText
if (tName == "EventDate") {
item.eventDate = event
}
if (tName == "EventDescription") {
item.eventDescription = event
}
}
Waybill.trackingEvents.add(item)
}
您应该研究Kotlin的命名约定;字段从不以大写字母开头,除非它是一个静态常量(在这种情况下为全大写)
答案 1 :(得分:3)
由于TrackingEventsList
是一个对象,因此它意味着是一个单例(或只有一个实例)。遍历循环时,您总是在更新TrackingEventsList
对象的相同实例
将TrackingEventsList
更改为此:
data class TrackingEventsList(var eventDate: String, var eventDescription: String)
每次循环时创建一个新实例,然后将其添加到列表的末尾:
private fun fillTracking(events: NodeList) {
var eventDate: String = ""
var eventDescription: String = ""
for (x: Int in 0 until events.length) {
val tName = (events.item(x) as Element).tagName
val event = (events.item(x).firstChild as Text).wholeText
if (tName == "EventDate") {
eventDate = event
}
if (tName == "EventDescription") {
eventDescription = event
}
}
Waybill.TrackingEvents.plus(TrackingEventsList(eventDate, eventDescription))
}