如何从 Firestore (Kotlin) 中的嵌套地图数组中为 RecyclerView 检索数据
How to retrieve data from nested array of maps in Firestore (Kotlin) for RecyclerView
我想寻求帮助,了解如何从 Firestore 中检索名为“城市”的嵌套地图数组到 MutableList 中的数据,然后我想将其插入到回收器视图中,其中来自“区域”的数据是对于常规列表项的 header 和数据“城市”。
区域数据:MutableList,当我按照 Alex Mamo 的程序 https://medium.com/firebase-tips-tricks/how-to-map-an-array-of-objects-from-cloud-firestore-to-a-list -of-objects-122e579eae10 进行操作时,一切正常,但数据:城市:MutableList,根据相同的方法,为空(无法检索)。
请问如何获取“城市”的数据?
P.s。我在某处阅读了迭代“城市”的建议,但我不知道如何,请直接举个例子(最好是在 Kontlin)。
代码:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
…..
regionsRef.get().addOnCompleteListener { document ->
if (document.isSuccessful()) {
val documentSnapshot = document.result
// Retrieve array of maps for „regions“
val regions = documentSnapshot.toObject(RegionDocument::class.java)?.regions
// Retrieve array of maps for „cities“
val cities = documentSnapshot.toObject(CityDocument::class.java)?.cities
…
}
}
数据 类 object 城市:
data class City(
val cityNumber: Long? = null,
val cityName: String? = "" )
data class CityDocument(
var cities: MutableList<City>? = null)
Firestore 结构:
为了能够得到对应你文档结构的数据,你需要三个类:
class Document {
var regions: MutableList<Region>? = null
}
class Region {
var cities: MutableList<City>? = null
var regionName: String? = null
var regionNumber: Long? = null
}
class City {
var cityName: String? = null
var cityNumber: Long? = null
}
下面是读取所有城市的解决方案:
val db = FirebaseFirestore.getInstance()
val docIdRef = db.collection("collName").document("docId")
docIdRef.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
val document = task.result
if (document != null) {
val doc = document.toObject(Document::class.java)
if (doc != null) {
val regions = doc.regions
if (regions != null) {
for (region in regions) {
val cities = region.cities
//Do what you need to to do with your List<City>.
}
}
}
}
} else {
Log.d("TAG", task.exception!!.message!!) //Never ignore potential errors!
}
}
现在,只需将 collName
和 docId
替换为您数据库中的那个即可。
我想寻求帮助,了解如何从 Firestore 中检索名为“城市”的嵌套地图数组到 MutableList 中的数据,然后我想将其插入到回收器视图中,其中来自“区域”的数据是对于常规列表项的 header 和数据“城市”。
区域数据:MutableList,当我按照 Alex Mamo 的程序 https://medium.com/firebase-tips-tricks/how-to-map-an-array-of-objects-from-cloud-firestore-to-a-list -of-objects-122e579eae10 进行操作时,一切正常,但数据:城市:MutableList,根据相同的方法,为空(无法检索)。
请问如何获取“城市”的数据?
P.s。我在某处阅读了迭代“城市”的建议,但我不知道如何,请直接举个例子(最好是在 Kontlin)。
代码:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
…..
regionsRef.get().addOnCompleteListener { document ->
if (document.isSuccessful()) {
val documentSnapshot = document.result
// Retrieve array of maps for „regions“
val regions = documentSnapshot.toObject(RegionDocument::class.java)?.regions
// Retrieve array of maps for „cities“
val cities = documentSnapshot.toObject(CityDocument::class.java)?.cities
…
}
}
数据 类 object 城市:
data class City(
val cityNumber: Long? = null,
val cityName: String? = "" )
data class CityDocument(
var cities: MutableList<City>? = null)
Firestore 结构:
为了能够得到对应你文档结构的数据,你需要三个类:
class Document {
var regions: MutableList<Region>? = null
}
class Region {
var cities: MutableList<City>? = null
var regionName: String? = null
var regionNumber: Long? = null
}
class City {
var cityName: String? = null
var cityNumber: Long? = null
}
下面是读取所有城市的解决方案:
val db = FirebaseFirestore.getInstance()
val docIdRef = db.collection("collName").document("docId")
docIdRef.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
val document = task.result
if (document != null) {
val doc = document.toObject(Document::class.java)
if (doc != null) {
val regions = doc.regions
if (regions != null) {
for (region in regions) {
val cities = region.cities
//Do what you need to to do with your List<City>.
}
}
}
}
} else {
Log.d("TAG", task.exception!!.message!!) //Never ignore potential errors!
}
}
现在,只需将 collName
和 docId
替换为您数据库中的那个即可。