在Kotlin中使用凌空后的空阵列列表
我正在处理一个简单的应用程序,在该应用程序中,我会动态地在listView中添加元素,为此,我正在使用Alley读取JSON,然后在数据类中添加所有信息,正如我在其他问题中看到的,凌空时,当我何时我尝试获取我的列表的大小,它返回0,因为它是在凌空前执行的。
我无法弄清楚如何等待凌空结束,然后再继续使用Kotlin中的代码,这是我获取数据的代码:
private fun getUsers() {
val url = ""
val requestQueue = Volley.newRequestQueue(this)
val request = JsonObjectRequest(Request.Method.GET, url, null, {
response ->try {
val jsonArray = response.getJSONArray("" + "data")
for (i in 0 until jsonArray.length()) {
val distr = jsonArray.getJSONObject(i)
val getUser = distr.getString("Utilizador").toString()
val getCargo = distr.getString("Cargo").toString()
val getEstado = distr.getString("Estado").toString().toInt()
listUtilizadores.add(Utilizadores(getUser, getCargo, getEstado))
}
} catch (e: JSONException) {
e.printStackTrace()
}
}, { error -> error.printStackTrace() })
requestQueue.add(request)
}
以及我如何在OnCreate()中称其为
getUsers()
Log.i("Testing", "Value: " + listUtilizadores.size)
Tonge Tonge you
I'm working on a simple app where I add elements in a ListView dynamically, for this I'm using Volley to read JSON and then add all the information inside a data class, as I saw in other questions Volley is asynchronous so when I try to get the size of my list it return 0 because it was executed before Volley.
I can't figure out how to wait for volley to finish before continuing with the code in Kotlin, this is the code where I get the data:
private fun getUsers() {
val url = ""
val requestQueue = Volley.newRequestQueue(this)
val request = JsonObjectRequest(Request.Method.GET, url, null, {
response ->try {
val jsonArray = response.getJSONArray("" + "data")
for (i in 0 until jsonArray.length()) {
val distr = jsonArray.getJSONObject(i)
val getUser = distr.getString("Utilizador").toString()
val getCargo = distr.getString("Cargo").toString()
val getEstado = distr.getString("Estado").toString().toInt()
listUtilizadores.add(Utilizadores(getUser, getCargo, getEstado))
}
} catch (e: JSONException) {
e.printStackTrace()
}
}, { error -> error.printStackTrace() })
requestQueue.add(request)
}
And how I call it inside onCreate()
getUsers()
Log.i("Testing", "Value: " + listUtilizadores.size)
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试Jonarrayrequest吗?
Can you try with JsonArrayRequest.