简体   繁体   English

将对象从类传递给活动

[英]Pass object from a Class to Activity

I want to send threeHourForecast object from onSuccess method back to activity from which it is called.我想将来自onSuccess方法的ThreeHourForecast 对象发送回调用它的活动。 I want clean and best solution for this problem.我想要这个问题的干净和最佳解决方案。 Here is my code.这是我的代码。

Weather Forecast Handler Class:天气预报处理程序类:

open class WeatherForecastHandler {

open fun getForecast(lat: Double, lng: Double, weatherKey: String){
    val helper = OpenWeatherMapHelper(weatherKey)
    helper.setUnits(Units.METRIC)
    helper.setLang(Lang.ENGLISH)

    helper.getThreeHourForecastByGeoCoordinates(lat, lng, object : ThreeHourForecastCallback {
        override fun onSuccess(threeHourForecast: ThreeHourForecast) {//send this "threeHourForecast" object back to the place from which "getForecast()" method is called.}

        override fun onFailure(throwable: Throwable) {
            Log.d("forecast", throwable.message!!)
        }
    })
}

} }

Calling Function Place:调用函数位置:

Maps Activity Class:地图活动类:

open class MapsActivity : FragmentActivity(), OnMapReadyCallback{

private lateinit var googleMap: GoogleMap
private lateinit var startPoint: LatLng

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_maps)

    val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
    mapFragment!!.getMapAsync(this)

    val bundle: Bundle? = intent.getParcelableExtra("bundle")
    startPoint = bundle!!.getParcelable("startPoint")!!
}

override fun onMapReady(map: GoogleMap?) {
    googleMap = map!!
    val weatherHandler = WeatherForecastHandler()
    weatherHandler.getForecast(startPoint.latitude, startPoint.longitude, getString(R.string.key)
//I need object here.
}

Try to adding a function in your function type parameter.尝试在您的函数类型参数中添加一个函数。 Like,喜欢,

Weather Forecast Handler Class:天气预报处理程序类:

open fun getForecast(lat: Double, lng: Double, weatherKey: String, callback: ((result: ThreeHourForecast?) -> Unit)){
    val helper = OpenWeatherMapHelper(weatherKey)
    helper.setUnits(Units.METRIC)
    helper.setLang(Lang.ENGLISH)

    helper.getThreeHourForecastByGeoCoordinates(lat, lng, object : ThreeHourForecastCallback {
        override fun onSuccess(threeHourForecast: ThreeHourForecast) {//send this "threeHourForecast" object back to the place from which "getForecast()" method is called.
         callback(threeHourForecast)
        }

        override fun onFailure(throwable: Throwable) {
         callback(null)
        }
    })
}

Maps Activity Class:地图活动类:

override fun onMapReady(map: GoogleMap?) {
    googleMap = map!!
    val weatherHandler = WeatherForecastHandler()
    weatherHandler.getForecast(startPoint.latitude, startPoint.longitude, getString(R.string.key) { result: ThreeHourForecast? ->
 // You can now receive value of 'threeHourForecast'
}
//I need object here.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM