Android 如何访问存储在kotlin块中的变量中的值
我有一个使用for循环填充的列表。我在具有此块的方法中运行了此代码。我需要有关如何返回myReportLocations及其值的帮助Android 如何访问存储在kotlin块中的变量中的值,android,kotlin,Android,Kotlin,我有一个使用for循环填充的列表。我在具有此块的方法中运行了此代码。我需要有关如何返回myReportLocations及其值的帮助 private fun getReportCoordinates(): ArrayList<HashMap<String, String>>{ var reportLocations : ArrayList<HashMap<String, String>> = ArrayList()
private fun getReportCoordinates(): ArrayList<HashMap<String, String>>{
var reportLocations : ArrayList<HashMap<String, String>> = ArrayList()
var myReportLocations : ArrayList<HashMap<String, String>> = ArrayList()
Fuel.get("http://kite.pythonanywhere.com/api/v1/progress").header(Headers.COOKIE, cookie).responseJson { request: Request, response: Response, result: Result<FuelJson, FuelError> ->
//incident_count = (count_occurrences('{', response.body().toString()) - 1).toString()
for (i in 0..result.get().array().length() -1){
//myReportLat = result.get().array().getJSONObject(i)["latitude"].toString()
//myReportLong= result.get().array().getJSONObject(i)["longitude"].toString()
var reportCoord: HashMap<String, String> = HashMap()
//println("The coordinates are: " + myReportLat+ " : " + myReportLong)
reportCoord.put("LocationID", i.toString())
reportCoord.put("Latitude", result.get().array().getJSONObject(i)["latitude"].toString())
reportCoord.put("Longitude", result.get().array().getJSONObject(i)["longitude"].toString())
reportCoord.put("LocationName", "Progress Report")
reportLocations.add(reportCoord)
}
/*for (x in 0..reportLocations.size -1){
println("This is the " + x + "th coordinate " + reportLocations.get(x))
}*/
myReportLocations.addAll(reportLocations)//everything is stored here
}
myReportLocations//there nothing stored
return myReportLocations
}
private fun getReportCoordinates():ArrayList{
var reportLocations:ArrayList=ArrayList()
var myReportLocations:ArrayList=ArrayList()
燃料。获取(“http://kite.pythonanywhere.com/api/v1/progressheader(Headers.COOKIE,COOKIE).responseJson{request:request,response:response,result:result->
//事件计数=(事件计数('{',response.body().toString())-1.toString()
for(0..result.get().array().length()-1中的i){
//myReportLat=result.get().array().getJSONObject(i)[“纬度”].toString()
//myReportLong=result.get().array().getJSONObject(i)[“经度”].toString()
var reportCoord:HashMap=HashMap()
//println(“坐标为:“+myReportLat+”:“+myReportLong”)
reportCoord.put(“LocationID”,i.toString())
reportCoord.put(“纬度”,result.get().array().getJSONObject(i)[“纬度”].toString())
reportCoord.put(“经度”,result.get().array().getJSONObject(i)[“经度”].toString())
reportCoord.put(“位置名称”、“进度报告”)
reportLocations.add(reportCoord)
}
/*对于(0..reportLocations.size-1中的x){
println(“这是“+x+”th坐标“+reportLocations.get(x))
}*/
myReportLocations.addAll(reportLocations)//所有内容都存储在这里
}
myReportLocations//没有存储任何内容
返回myReportLocations
}
这是一个异步调用。因此列表将在返回后更新。
您要做的是将其转换为阻塞网络调用(尽管我会使用另一个线程,否则如果在主线程上执行此操作,您的应用程序将冻结)
尝试:
private fun getReportCoordinates():ArrayList{
var reportLocations:ArrayList=ArrayList()
var myReportLocations:ArrayList=ArrayList()
val结果=燃料。获取(“http://kite.pythonanywhere.com/api/v1/progress“”.header(Headers.COOKIE,COOKIE.responseJson()
//事件计数=(事件计数('{',response.body().toString())-1.toString()
for(0..result.third.get().array().length()-1中的i){
//myReportLat=result.get().array().getJSONObject(i)[“纬度”].toString()
//myReportLong=result.get().array().getJSONObject(i)[“经度”].toString()
var reportCoord:HashMap=HashMap()
//println(“坐标为:“+myReportLat+”:“+myReportLong”)
reportCoord.put(“LocationID”,i.toString())
reportCoord.put(“纬度”,result.get().array().getJSONObject(i)[“纬度”].toString())
reportCoord.put(“经度”,result.get().array().getJSONObject(i)[“经度”].toString())
reportCoord.put(“位置名称”、“进度报告”)
reportLocations.add(reportCoord)
}
myReportLocations.addAll(reportLocations)
返回myReportLocations
}以下是您的案例的解决方案:
private fun getReportCoordinates(result: (list:ArrayList<HashMap<String, String>> -> Unit) {
var reportLocations : ArrayList<HashMap<String, String>> = ArrayList()
var myReportLocations : ArrayList<HashMap<String, String>> = ArrayList()
Fuel.get("http://kite.pythonanywhere.com/api/v1/progress").header(Headers.COOKIE, cookie).responseJson { request: Request, response: Response, result: Result<FuelJson, FuelError> ->
//incident_count = (count_occurrences('{', response.body().toString()) - 1).toString()
for (i in 0..result.get().array().length() -1){
//myReportLat = result.get().array().getJSONObject(i)["latitude"].toString()
//myReportLong= result.get().array().getJSONObject(i)["longitude"].toString()
var reportCoord: HashMap<String, String> = HashMap()
//println("The coordinates are: " + myReportLat+ " : " + myReportLong)
reportCoord.put("LocationID", i.toString())
reportCoord.put("Latitude", result.get().array().getJSONObject(i)["latitude"].toString())
reportCoord.put("Longitude", result.get().array().getJSONObject(i)["longitude"].toString())
reportCoord.put("LocationName", "Progress Report")
reportLocations.add(reportCoord)
}
/*for (x in 0..reportLocations.size -1){
println("This is the " + x + "th coordinate " + reportLocations.get(x))
}*/
result(myReportLocations)
}
}
private fun getReportCoordinates(结果:(列表:ArrayList->Unit){
var reportLocations:ArrayList=ArrayList()
var myReportLocations:ArrayList=ArrayList()
燃料。获取(“http://kite.pythonanywhere.com/api/v1/progressheader(Headers.COOKIE,COOKIE).responseJson{request:request,response:response,result:result->
//事件计数=(事件计数('{',response.body().toString())-1.toString()
for(0..result.get().array().length()-1中的i){
//myReportLat=result.get().array().getJSONObject(i)[“纬度”].toString()
//myReportLong=result.get().array().getJSONObject(i)[“经度”].toString()
var reportCoord:HashMap=HashMap()
//println(“坐标为:“+myReportLat+”:“+myReportLong”)
reportCoord.put(“LocationID”,i.toString())
reportCoord.put(“纬度”,result.get().array().getJSONObject(i)[“纬度”].toString())
reportCoord.put(“经度”,result.get().array().getJSONObject(i)[“经度”].toString())
reportCoord.put(“位置名称”、“进度报告”)
reportLocations.add(reportCoord)
}
/*对于(0..reportLocations.size-1中的x){
println(“这是“+x+”th坐标“+reportLocations.get(x))
}*/
结果(myReportLocations)
}
}
myReportLocations为空的原因是填充它的代码段(即回调)异步执行,以便在执行回调之前完成getReportCoordinates
。例如,为了获得正确的结果,可以从回调中调用具有适当位置的函数
private fun getReportCoordinates(result: (list:ArrayList<HashMap<String, String>> -> Unit) {
var reportLocations : ArrayList<HashMap<String, String>> = ArrayList()
var myReportLocations : ArrayList<HashMap<String, String>> = ArrayList()
Fuel.get("http://kite.pythonanywhere.com/api/v1/progress").header(Headers.COOKIE, cookie).responseJson { request: Request, response: Response, result: Result<FuelJson, FuelError> ->
//incident_count = (count_occurrences('{', response.body().toString()) - 1).toString()
for (i in 0..result.get().array().length() -1){
//myReportLat = result.get().array().getJSONObject(i)["latitude"].toString()
//myReportLong= result.get().array().getJSONObject(i)["longitude"].toString()
var reportCoord: HashMap<String, String> = HashMap()
//println("The coordinates are: " + myReportLat+ " : " + myReportLong)
reportCoord.put("LocationID", i.toString())
reportCoord.put("Latitude", result.get().array().getJSONObject(i)["latitude"].toString())
reportCoord.put("Longitude", result.get().array().getJSONObject(i)["longitude"].toString())
reportCoord.put("LocationName", "Progress Report")
reportLocations.add(reportCoord)
}
/*for (x in 0..reportLocations.size -1){
println("This is the " + x + "th coordinate " + reportLocations.get(x))
}*/
result(myReportLocations)
}
}