我的应用程序调用一个名为getGroupsInfo的云函数。它返回3个值:result
、message
和groupsInfo
。
在Logcat中,我打印出了3个值,下面是结果。我现在需要使用groupsInfo
数组中的每个元素,但我不确定如何访问每个元素。在下面的示例中,我尝试访问第一个元素,但没有成功。
fs.getGroupsInfo()
.continueWith { task ->
if (task.isSuccessful) {
Log.e(tag, "Result1: ${task.result?.get("result")}") //case sensitive
Log.e(tag, "Result2: ${task.result?.get("message")}") //case sensitive
Log.e(tag, "GroupsInfo: ${task.result?.get("groupsInfo")}") //case sensitive
val result = task.result?.get("result")
val message = task.result?.get("message")
if (result == 1) {
var groupsInfo: Array<Any?> = arrayOf(task.result?.get("groupsInfo"))
Log.e(tag, groupsInfo[0].toString())
} else {
Log.e(tag, "Loading failed.")
Toast.makeText(baseContext, "Could not load groups.", Toast.LENGTH_SHORT).show()
}
} else {
Log.e(tag, "Result3: ${task.result?.get("result")}") //case sensitive
Log.e(tag, "Result4: ${task.result?.get("message")}") //case sensitive
Toast.makeText(baseContext, "There was a system error while trying to load the groups.", Toast.LENGTH_SHORT).show()
}
}
2021-05-09 20:13:33.061 8512-8512/com.example.2 E/_ActivityGroups: Result1: 1
2021-05-09 20:13:33.061 8512-8512/com.example.2 E/_ActivityGroups: Result2:
2021-05-09 20:13:33.061 8512-8512/com.example.2 E/_ActivityGroups: GroupsInfo: [73Xwaz7vAOM39e1KVPu8, NOti0G0Me0VSte48I2O0]
2021-05-09 20:13:33.061 8512-8512/com.example.2 E/_ActivityGroups: Loading groups was successful!
2021-05-09 20:13:33.062 8512-8512/com.example.2 E/_ActivityGroups: [73Xwaz7vAOM39e1KVPu8, NOti0G0Me0VSte48I2O0]
在我的方法中,访问元素的简单方法是循环的迭代
task.result?.get("groupInfo").toList().map { /**incase it's not string*/ it.toString() } .forEach {
Log.d(tag, "$it")
}