如果有人正在寻找答案,这就是我使用 Kotlin 实现它的方式。
可打包对象类
data class CollectedMilk(
@SerializedName("id")
var id: Int,
@SerializedName("igicuba")
var igicuba: Int,
@SerializedName("collector")
var collector: String?,
@SerializedName("collected")
var collected: Int,
@SerializedName("accepted")
var accepted: Int,
@SerializedName("standard")
var standard: String?,
@SerializedName("created_at")
var created_at: String?,
@SerializedName("updated_at")
var updated_at: String?,
): Parcelable {
constructor(parcel: Parcel) : this(
parcel.readInt(),
parcel.readInt(),
parcel.readString(),
parcel.readInt(),
parcel.readInt(),
parcel.readString(),
parcel.readString(),
parcel.readString()
)
override fun describeContents(): Int {
return 0
}
override fun writeToParcel(parcel: Parcel?, int: Int) {
parcel?.writeInt(id)
parcel?.writeInt(igicuba)
parcel?.writeString(collector)
parcel?.writeInt(collected)
parcel?.writeInt(accepted)
parcel?.writeString(standard)
parcel?.writeString(created_at)
parcel?.writeString(updated_at)
}
companion object CREATOR : Parcelable.Creator<CollectedMilk> {
override fun createFromParcel(parcel: Parcel): CollectedMilk {
return CollectedMilk(parcel)
}
override fun newArray(size: Int): Array<CollectedMilk?> {
return arrayOfNulls(size)
}
}
}
然后在片段中
collectedMilkAdapter.onItemClick = { collectedMilk ->
Toast.makeText(
MccApp.applicationContext(),
"Collector: " + collectedMilk.collector,
Toast.LENGTH_LONG
).show()
val intent = Intent(MccApp.applicationContext(), CollectedMilkActivity::class.java)
val bundle: Bundle = Bundle()
bundle.putParcelableArrayList("collectedMilk", collectedMilkArrayList)
intent.putExtras(bundle)
activity?.startActivity(intent)
}
然后在另一个活动上我的详细活动
全局变量
private lateinit var collectedMilk: ArrayList<CollectedMilk>
在函数或 OnCreate 活动中
val bundle = intent.extras
collectedMilk = (bundle?.getParcelableArrayList<CollectedMilk>("collectedMilk") as ArrayList<CollectedMilk>)
Toast(this).showCustomToast(
this,
""+collectedMilk,
dark
)