如何将对象从活动传递到片段?

时间:2018-07-18 08:13:21

标签: android json android-fragments parcelable

我想将对象(通过改造获得)传递给片段。我听说过两种方法,但是两种方法都有问题。我试图将FullForecast对象传递给片段。

  1. 使用Parcelize。我在班上实现了它,但它与构造函数冲突。

enter image description here

  1. 我听说我可以将Json字符串从活动传递到片段,然后将其转换为片段内的对象。但是,我无法从Json调用中获得Json字符串。我尝试了Call<ResponseBody>,并做了response.body().toString(),但是没有得到Json字符串

这是我的代码

repository.getWeatherForecast(place,
                object : Callback<FullForecast> {
                    override fun onFailure(call: Call<FullForecast>?, t: Throwable?) {
                        println("onFailure")
                    }

                    override fun onResponse(call: Call<FullForecast>?, response: Response<FullForecast>?) {
                        if (response != null && response.isSuccessful && response.body() != null) {

                            forecastObj = response.body() as FullForecast
                            // Try to get Json string here
                        }
                    }
                })

@JsonClass(generateAdapter = true)
data class FullForecast(@Json(name = "list")
                        val forecastList: List<WeatherForecast>) {
}

@JsonClass(generateAdapter = true)
data class WeatherForecast(@Json(name = "main")
                           val weatherDetail: WeatherDetail,

                           @Json(name = "weather")
                           val weatherIcon: List<WeatherIcon>,

                           @Json(name = "dt_txt")
                           val date: String) {
}

@JsonClass(generateAdapter = true)
data class Place(@Json(name = "main")
                 val weatherDetail: WeatherDetail,

                 @Json(name = "weather")
                 val weatherIcon: List<WeatherIcon>,

                 @Json(name = "sys")
                 val countryDetail: CountryDetail,

                 @Json(name = "dt_txt")
                 var forecastDate: String = "",

                 val name: String,

                 var placeIdentifier: String = "",

                 var lastUpdated: String = "") {
}

@JsonClass(generateAdapter = true)
data class CountryDetail(val country: String) {
}

@JsonClass(generateAdapter = true)
data class WeatherDetail(@Json(name = "temp")
                         val temperature: Double,
                         val temp_min: Double,
                         val temp_max: Double) {
}

@JsonClass(generateAdapter = true)
data class WeatherIcon(val icon: String) {
}

3 个答案:

答案 0 :(得分:0)

尝试EventBus将对象传递给片段。

答案 1 :(得分:0)

您应按以下方式实施Parcelize

  @Parcelize
  @JsonClass(generateAdapter = true)
  data class FullForecast(@Json(name = "list")
  val forecastList: List<WeatherForecast>) : Parcelable {
  }

  @Parcelize
  @JsonClass(generateAdapter = true)
  data class WeatherForecast(@Json(name = "main")
  val weatherDetail: WeatherDetail,

      @Json(name = "weather")
      val weatherIcon: List<WeatherIcon>,

      @Json(name = "dt_txt")
      val date: String) : Parcelable {
  }

  @Parcelize
  @JsonClass(generateAdapter = true)
  data class Place(@Json(name = "main")
  val weatherDetail: WeatherDetail,

      @Json(name = "weather")
      val weatherIcon: List<WeatherIcon>,

      @Json(name = "sys")
      val countryDetail: CountryDetail,

      @Json(name = "dt_txt")
      var forecastDate: String = "",

      val name: String,

      var placeIdentifier: String = "",

      var lastUpdated: String = "") : Parcelable {
  }

  @Parcelize
  @JsonClass(generateAdapter = true)
  data class CountryDetail(val country: String) : Parcelable {
  }

  @Parcelize
  @JsonClass(generateAdapter = true)
  data class WeatherDetail(@Json(name = "temp")
  val temperature: Double,
      val temp_min: Double,
      val temp_max: Double) : Parcelable {
  }

  @Parcelize
  @JsonClass(generateAdapter = true)
  data class WeatherIcon(val icon: String) : Parcelable {
  }

如果遇到错误:

enter image description here

这是IDE本身的一个已知错误,您可以忽略它,代码没有错,并且可以按预期运行。您可以跟踪问题here

答案 2 :(得分:0)

遵循

步骤1: 使您的对象实现可序列化

例如public class Match implements Serializable { }

第2步:将您的对象捆绑在一起,然后将其传递给Activity或Fragment

例如。

Bundle args = new Bundle();
args.putSerializable("ARG_PARAM1", yourObject);

第3步:像这样获取对象表单包

 yourObj = (Match) getArguments().getSerializable(ARG_PARAM1);