-4
kotlinでパーセル化可能なオブジェクトを作成する最も良い方法は?kotlinでパーセル化可能なオブジェクトを作成する
kotlinでパーセル化可能なオブジェクトを作成する最も良い方法は?kotlinでパーセル化可能なオブジェクトを作成する
TestActivity.kt
class TestActivity : Activity() {
val DEVOTIONAL_RESPONSE: String = "DEVOTIONAL_RESPONSE"
var holdr: Holdr_ActivityTest? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test)
holdr = Holdr_ActivityTest(findViewById(android.R.id.content))
//create response
var response: DevotionalResponse = DevotionalResponse()
fillInResponse(response)
//write it to a Bundle
var newBundle: Bundle = Bundle()
newBundle.putParcelable(DEVOTIONAL_RESPONSE, response)
//read it from the new Bundle
var newResponse: DevotionalResponse = newBundle.getParcelable(DEVOTIONAL_RESPONSE)
//check that they're the same
assert(newResponse.getDevotionals().get(0).getContent().equalsIgnoreCase(response.getDevotionals().get(0).getContent()))
}
}
DevotionalResponse.kt
class DevotionalResponse() : Parcelable {
@Expose
var devotionals = arrayListOf<Devotional>();
constructor(in: Parcel) : this() {
in.readTypedList(this.devotionals, Devotional.CREATOR)
}
override fun describeContents(): Int = 0
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeTypedList(devotionals)
}
companion object {
@JvmField val CREATOR = object : Parcelable.Creator<DevotionalResponse>() {
override fun createFromParcel(in: Parcel): DevotionalResponse =
DevotionalResponse(in)
override fun newArray(size: Int): Array<DevotionalResponse?> =
arrayOfNulls(size)
}
}
}
完全なソリューションは、このリンクに行く
*この*は、オブジェクトを渡すためのAndroidの方法です。受け入れられた回答としてマークする必要があります。 –
@ Miha_x64 yaaaしかし、あなたはkotlinでそれをすることができます – Sendy
ああ、私はそれがJavaで気づいたことはありません。 KotlinとJavaの切り替えは、私にとっては絶対にシームレスです。 :) –