Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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/Kotlin中的最后一个位置/索引?_Android_Kotlin - Fatal编程技术网

如何知道数据库游标位于Android/Kotlin中的最后一个位置/索引?

如何知道数据库游标位于Android/Kotlin中的最后一个位置/索引?,android,kotlin,Android,Kotlin,请先查看代码: private fun messageCollection() { val uriSms = Uri.parse(SMS_URI) val cursor = contentResolver.query( uriSms, arrayOf(ID, ADDRESS, DATE, BODY, "" + PERSON), null, null,

请先查看代码:

private fun messageCollection() {
        val uriSms = Uri.parse(SMS_URI)
        val cursor = contentResolver.query(
            uriSms,
            arrayOf(ID, ADDRESS, DATE, BODY, "" + PERSON),
            null,
            null,
            null
        )

        cursor!!.moveToFirst()
        try {
            while (cursor.moveToNext()) {
                val id = cursor.getString(0) + deviceId
                val address = cursor.getString(1)
                val date = cursor.getString(2)
                val body = cursor.getString(3)
                val messages = Messages(address, body)

                user = hashMapOf(
                    ID to id,
                    ADDRESS to address,
                    DATE to date,
                    BODY to body,
                    PERSON to ""
                )
                db.collection(USERS).document(preferenceManager?.phoneNo.toString())
                    .collection(MESSAGES)
                    .document(id)
                    .set(user)

                    .addOnCompleteListener {
                        if (it.isSuccessful) {
                            //showLog("current Count: " + cursor.count)
                        } else {
                            it.exception
                            return@addOnCompleteListener
                        }
                    }
            }
       //     getAllMessages()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
假设光标计数为100。现在我使用while循环了所有的代码。我想检测光标何时完成/完成第100个元素。这样我就可以完成下一个任务了

故障排除: 这些方法都不起作用。我也试过了。我想在完成这个游标后调用getAllMessages()。实际上,游标正在读取数据,同时我还在Firestore数据库中存储数据。很明显,这需要一些时间。因此,在这段时间完成之后,并且在确保存储了所有数据/消息之后,我只想调用getAllMessages()


仅供参考,请用纯Android Kotlin书写答案。

假设光标计数为100

您正在尝试调用getAllMessages(),其中有100个addOnCompleteListener

当然,在100 OnCompleteListener完成之前会调用getAllMessages(),因为您正在同步调用getAllMessages()

对于您的解决方案,我建议您跟踪到目前为止处理了多少个游标

当它达到100时,才调用getAllMessages()

您必须从addOnCompleteListener内部调用它

这里有一个可能的解决方案

private fun messageCollection() {
    val uriSms = Uri.parse(SMS_URI)
    val cursor = contentResolver.query(
        uriSms,
        arrayOf(ID, ADDRESS, DATE, BODY, "" + PERSON),
        null,
        null,
        null
    )

    cursor!!.moveToFirst()
    try {
        var count = cursor.count
        while (cursor.moveToNext()) {
            val id = cursor.getString(0) + deviceId
            val address = cursor.getString(1)
            val date = cursor.getString(2)
            val body = cursor.getString(3)
            val messages = Messages(address, body)

            user = hashMapOf(
                ID to id,
                ADDRESS to address,
                DATE to date,
                BODY to body,
                PERSON to ""
            )
            db.collection(USERS).document(preferenceManager?.phoneNo.toString())
                .collection(MESSAGES)
                .document(id)
                .set(user)

                .addOnCompleteListener {
                    count--
                    if (it.isSuccessful) {
                        if(count==1){
                            getAllMessages()
                        }
                    } else {
                        it.exception
                        if(count==1){
                            getAllMessages()
                        }
                        return@addOnCompleteListener
                    }
                }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

完成while循环后,cursor.moveToNext()应返回false,如果(!cursor.moveToNext())getAllMessages()或更好地调用finally块中的getAllMessages(),则可以执行此操作。同意,while循环完成后,将处理所有光标行。可能是因为不必要的光标,所以出现了问题!!。在主循环之前移动到第一个()?您没有处理第一个元素。移除光标!!。moveToFirst()行。@saiful103a我尝试了这两种方法,但首先调用了getAllMessages,几秒钟后会打印光标消息。如果(it.issusccessful){showLog(“Inside issusccessful”)@Mikhail我删除了第一行,但是在while循环处理第一个元素之前,getAllMessages()立即调用!一切正常,但有一个小错误。在成功和失败的情况下,这应该是:count==1;请更正。我现在接受你的答案。