微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在kotlin中同步函数中的所有代码

如何解决如何在kotlin中同步函数中的所有代码

当我调用函数时,我有一个具有一些“for 循环”的函数,然后第二个“for 循环”在第一个“for 循环”之前执行。在第一个“for 循环”中,我调用一个方法并有一个调用方向 API 的代码。现在我想停止代码的执行,直到第一个 for 循环完成它的执行。我在谷歌上搜索,但没有找到解决方案。请帮助我如何做到这一点的人。下面我贴出我的代码,请看一下。

private  fun setAllStopsInRange(stops: ArrayList<LatLng>) {
    arraylistofRouteOriginMarker.forEach { it.remove() }
    stops.forEach { 
        arraylistofRouteOriginMarker.add(map.addMarker(
            MarkerOptions().icon(BitmapDescriptorFactory.fromresource(R.drawable.blue_car))
                .position(
                    it
                )
        ))
    }
    RouteDestinationMarker?.remove()
    arraylistofArea!!.forEach {
        if (it.areaName == binding.chooseAreaSpinner.selectedItem.toString()){
            destinationLatLong = it.areaLatLong
            RouteDestinationMarker = map.addMarker(MarkerOptions().position(destinationLatLong!!).title("Destination"))
        }
    }
    arraylistofAllRoutepolylines.forEach { it.remove() }
    for (i in stops.indices){
        if ((clickedRouteOriginMarkerLatLong != null) && (clickedRouteOriginMarkerLatLong == stops[i]) && (i != stops.lastIndex)){
            Collections.swap(stops,stops.lastIndex,i)
        }
    }

    hashMapOfstopsLatLong = HashMap<LatLng,ArrayList<LocationInfo>>()


    
    stops.forEach {
        arraylistofStopsLatLong = ArrayList<LocationInfo>()
        createRouteFromEachStop(it) // In this method I am calling api
    }

    for (latLong in arraylistofRouteOriginInRangeLatLong.iterator()){
        Log.i(TAG,"setAllStopsInRange: sidf "+arraylistofAllRouteOriginLatLong.size+","+arraylistofAllRouteOriginLatLong)
        var geocoder = Geocoder(this,Locale.getDefault())
        var addresses = geocoder.getFromLocation(
            latLong.latitude,latLong.longitude,1
        )

        val view = layoutInflater.inflate(R.layout.location_info_view,null)
        view.findViewById<TextView>(R.id.address_tv).text =  (addresses.get(0).subLocality+","+addresses.get(0).locality)
        view.findViewById<TextView>(R.id.time_tv).text =  "7:00 AM"
        binding.locationInfoView.addView(view)

    }
    Log.i(TAG,"setAllStopsInRange: ddifesize "+hashMapOfstopsLatLong)
    hashMapOfstopsLatLong.forEach { t,u ->    // this for loop execute before above for loop
        Log.i(TAG,"setAllStopsInRange: tjjis "+t+",u is "+u) 
        u.forEach {
            val view = layoutInflater.inflate(R.layout.location_info_view,null)
            view.findViewById<TextView>(R.id.address_tv).text = it.addressInfo
            view.findViewById<TextView>(R.id.time_tv).text =  it.time
            binding.locationInfoView.addView(view)
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。