Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用firebase获取循环中的当前计数?_Android_Firebase_Loops_Kotlin_Google Cloud Firestore - Fatal编程技术网

Android 如何使用firebase获取循环中的当前计数?

Android 如何使用firebase获取循环中的当前计数?,android,firebase,loops,kotlin,google-cloud-firestore,Android,Firebase,Loops,Kotlin,Google Cloud Firestore,我正在使用Firebase FireStore检索我的所有收集数据 但是我想打印每5个interwal的日志,如何获得循环的当前计数 如果有任何改变循环的建议,请与代码分享您的解决方案 这是我的密码 db .collection("news") .orderBy("timestamp", Query.Direction.DESCENDING) .whereLessThan("timestamp&qu

我正在使用Firebase FireStore检索我的所有收集数据 但是我想打印每5个interwal的日志,如何获得循环的当前计数

如果有任何改变循环的建议,请与代码分享您的解决方案

这是我的密码

db
        .collection("news")
        .orderBy("timestamp", Query.Direction.DESCENDING)
        .whereLessThan("timestamp",tm)
        .limit(10)
        .get()
        .addOnSuccessListener {
            for (doc in it) {
                val imgUrl = doc.getString("imageUrl")
                val heading = doc.getString("headline")
                val timestamp = doc.getTimestamp("timestamp")
                val tagline = doc.getString("tagline")
                val type = doc.getString("type")
                // position%5 but how to get the current position?
            }
        }

QuerySnapshot及其迭代器在迭代时不公开任何索引信息。您必须自己跟踪它:

int i = 0
for (doc in it) {
    if (++i % 5 == 0) {
        // multiple of 5
    }
}

或者,您可以迭代从中获取的快照列表,这将公开索引。

在迭代过程中,QuerySnapshot及其迭代器不会公开任何索引信息。您必须自己跟踪它:

int i = 0
for (doc in it) {
    if (++i % 5 == 0) {
        // multiple of 5
    }
}
 for ((i, doc) in it.withIndex()) {
                val imgUrl = doc.getString("imageUrl")
                val heading = doc.getString("headline")
                val timestamp = doc.getTimestamp("timestamp")
                val tagline = doc.getString("tagline")
                val type = doc.getString("type")
                if(i%5==0) {
                    //add ur item
                }
或者,您可以迭代从中获取的快照列表,这将公开索引

 for ((i, doc) in it.withIndex()) {
                val imgUrl = doc.getString("imageUrl")
                val heading = doc.getString("headline")
                val timestamp = doc.getTimestamp("timestamp")
                val tagline = doc.getString("tagline")
                val type = doc.getString("type")
                if(i%5==0) {
                    //add ur item
                }