代码之家  ›  专栏  ›  技术社区  ›  Zolly

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

  •  0
  • Zolly  · 技术社区  · 7 年前

    我想把对象(我通过改装得到的)传递给片段。我听说过两种方法,但这两种方法都有问题。我想通过 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 回复  |  直到 7 年前
        1
  •  0
  •   GianhTran    7 年前

    你应该实施 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本身的一个已知bug,您可以忽略它,代码没有任何错误,并且可以按预期工作。你可以跟踪这个问题 here .

        2
  •  0
  •   code4rox    7 年前

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

        3
  •  0
  •   Vishnu Magar    7 年前

    跟着这个走

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

    前任。 public class Match implements Serializable { }

    第2步:将对象打包并传递给活动或片段

    前任。

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

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

     yourObj = (Match) getArguments().getSerializable(ARG_PARAM1);
    
    推荐文章