简体   繁体   中英

How to retrieve data from nested array of maps in Firestore (Kotlin) for RecyclerView

I would like to ask for help on how to retrieve data from Firestore for nested Array of Maps called "cities" into MutableList, which I then want to insert into recycler view, where the data from the “regions” are for the header and data “cities” for the regular list items.

Data for regions: MutableList, when I follow the procedure https://medium.com/firebase-tips-tricks/how-to-map-an-array-of-objects-from-cloud-firestore-to-a-list -of-objects-122e579eae10 by Alex Mamo, got fine, but data for: cities: MutableList, according same approach, is null (unable to retrive).

Can you please advise how to get data for “cities”?

Ps somewhere I read the recommendation to iterate over "cities", but I have no idea how, please go straight for an example (ideally in Kontlin).

Code:

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

       …
    }
}

Data classes for object City:

data class City(
   val cityNumber: Long? = null,
   val cityName: String? = "" )

data class CityDocument(
    var cities: MutableList<City>? = null) 

Firestore structure:

Firestore 中的数据结构示例

突出显示的城市 - 地图数组

To be able to get the data that corresponds to your document structure, you need three classes:

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
}

And below you can find a solution for reading all cities:

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!
    }
}

Now, simply replace collName and docId with the one you have in your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM