我正在Android活动中从Firestore数据库中读取数据,并将数据存储在ArrayList
中,但是当我打印出ArrayList
时,我得到的是最后一个条目,而没有其他条目
这是我的Kotlin代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_counties)
var sites : ArrayList<Site> = ArrayList<Site>()
var site : Site = Site()
val selectedCounty: String = intent.getStringExtra("COUNTY")
sites.clear()
db.collection("UKSites")
.document("England")
.collection("Counties")
.document(selectedCounty)
.collection(selectedCounty)
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
site.name = document.data["Name"].toString()
site.address?.line1 = document.data["Address Line 1"].toString()
site.address?.line2 = document.data["Address Line 2"].toString()
site.address?.line3 = document.data["Address Line 3"].toString()
site.address?.line4 = document.data["Address Line 4"].toString()
site.address?.postcode = document.data["Postcode"].toString()
site.address?.phoneNumber = document.data["Telephone"].toString()
site.address?.siteURL = document.data["Site URL"].toString()
site.description = document.data["Description"].toString()
site.price = document.data["Price"] as Double
site.distance = 0
site.locationCoordinate?.Longitude = document.data["Longitude"] as Double
site.locationCoordinate?.Latitude = document.data["Latitude"] as Double
sites.add(site)
Log.i("Info","${sites.last().name}")
}
Log.i("Info", "${sites.size}")
for ( nextSite in sites ) {
Log.i("Info", "site.name = ${nextSite.name}")
}
// Recycler View code here
}
.addOnFailureListener { exception ->
Log.w("Error", "Error getting documents: ", exception)
}
这是输出到Logcat
窗口的输出:
2020-01-18 15:22:14.376 10629-10658/com.riverstonetech.gositeuk D/FA: Logging event (FE): screen_view(_vs), Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=CountriesActivity, ga_previous_id(_pi)=-7126357891560843792, ga_screen_class(_sc)=CountiesActivity, ga_screen_id(_si)=-7126357891560843791}]
2020-01-18 15:22:14.396 10629-10629/com.riverstonetech.gositeuk I/Info: Stroud Farm
2020-01-18 15:22:14.399 10629-10629/com.riverstonetech.gositeuk I/Info: Bennets Wood Farm
2020-01-18 15:22:14.401 10629-10629/com.riverstonetech.gositeuk I/Info: Bluebell Farm
2020-01-18 15:22:14.404 10629-10629/com.riverstonetech.gositeuk I/Info: Pinecopse Kennels
2020-01-18 15:22:14.407 10629-10629/com.riverstonetech.gositeuk I/Info: Pinecopse Kennels
2020-01-18 15:22:14.410 10629-10629/com.riverstonetech.gositeuk I/Info: Bennets Wood Farm
2020-01-18 15:22:14.414 10629-10629/com.riverstonetech.gositeuk I/Info: Bluebell Farm
2020-01-18 15:22:14.416 10629-10629/com.riverstonetech.gositeuk I/Info: Stroud Farm
2020-01-18 15:13:20.486 10221-10221/com.riverstonetech.gositeuk I/Info: 8
2020-01-18 15:13:20.486 10221-10221/com.riverstonetech.gositeuk I/Info: site.name = Stroud Farm
2020-01-18 15:13:20.486 10221-10221/com.riverstonetech.gositeuk I/Info: site.name = Stroud Farm
2020-01-18 15:13:20.486 10221-10221/com.riverstonetech.gositeuk I/Info: site.name = Stroud Farm
2020-01-18 15:13:20.486 10221-10221/com.riverstonetech.gositeuk I/Info: site.name = Stroud Farm
2020-01-18 15:13:20.486 10221-10221/com.riverstonetech.gositeuk I/Info: site.name = Stroud Farm
2020-01-18 15:13:20.486 10221-10221/com.riverstonetech.gositeuk I/Info: site.name = Stroud Farm
2020-01-18 15:13:20.486 10221-10221/com.riverstonetech.gositeuk I/Info: site.name = Stroud Farm
2020-01-18 15:13:20.487 10221-10221/com.riverstonetech.gositeuk I/Info: site.name = Stroud Farm
2020-01-18 15:13:20.502 10221-10252/com.riverstonetech.gositeuk D/EGL_emulation: eglMakeCurrent: 0xae085060: ver 3 0 (tinfo 0xae083400)
2020-01-18 15:13:20.524 10221-10252/com.riverstonetech.gositeuk D/EGL_emulation: eglMakeCurrent: 0xae085060: ver 3 0 (tinfo 0xae083400)
2020-01-18 15:13:20.578 10221-10252/com.riverstonetech.gositeuk D/EGL_emulation: eglMakeCurrent: 0xae085060: ver 3 0 (tinfo 0xae083400)
答案 0 :(得分:2)
您仅创建一次Site
对象。因此,在循环中使用时,您只是一次又一次地重置相同对象的值。这就是从循环中获取最后一个元素的值的原因。
您每次都需要创建对象。因此var site : Site = Site()
必须在for-each循环内。如下
for (document in documents) {
// Create object here
var site : Site = Site()
// Then do your tasks what is currently doing
site.name = document.data["Name"].toString()
....
}