如何将 activity 中的对象传递给片段?
How to pass objects from activity to fragments?
我想将对象(通过 Retrofit 获得)传递给片段。我听说过两种方法,但我都遇到了问题。我正在尝试将 FullForecast
对象传递给我的片段。
- 使用
Parcelize
。我在 class 上实现了它,但它与我的构造函数冲突。
- 我听说我可以将 activity 中的 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) {
}
尝试EventBus将对象传递给片段。
您应该按如下方式实施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 {
}
如果遇到错误:
这是 IDE 本身的一个已知错误,您可以忽略它,代码没有任何问题,并且按预期工作。您可以跟踪问题 here。
关注这个
第一步:
让你的对象实现 Serializable
例如public class Match implements Serializable {
}
第 2 步:将您的对象打包并传递给 Activity 或 Fragment
例如
Bundle args = new Bundle();
args.putSerializable("ARG_PARAM1", yourObject);
第 3 步:像这样获取对象表单包
yourObj = (Match) getArguments().getSerializable(ARG_PARAM1);
我想将对象(通过 Retrofit 获得)传递给片段。我听说过两种方法,但我都遇到了问题。我正在尝试将 FullForecast
对象传递给我的片段。
- 使用
Parcelize
。我在 class 上实现了它,但它与我的构造函数冲突。
- 我听说我可以将 activity 中的 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) {
}
尝试EventBus将对象传递给片段。
您应该按如下方式实施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 {
}
如果遇到错误:
这是 IDE 本身的一个已知错误,您可以忽略它,代码没有任何问题,并且按预期工作。您可以跟踪问题 here。
关注这个
第一步: 让你的对象实现 Serializable
例如public class Match implements Serializable {
}
第 2 步:将您的对象打包并传递给 Activity 或 Fragment
例如
Bundle args = new Bundle();
args.putSerializable("ARG_PARAM1", yourObject);
第 3 步:像这样获取对象表单包
yourObj = (Match) getArguments().getSerializable(ARG_PARAM1);