Tag: 包裹

Kotlin中的Android Parcelable:在Parcelable数据类中找不到CREATOR

随着Kotlin RC的发布,我开始编写一个应用程序来学习它,但我不知道如何让Parcelable工作。 数据类: data class Project (val reponame:String, val username:String, val language:String, val vcsUrl:String, val branches:Map) : Parcelable { companion object { val CREATOR = object : Parcelable.Creator { override fun createFromParcel(`in`: Parcel): Project { return Project(`in`) } override fun newArray(size: Int): Array { return arrayOfNulls(size) } } } protected constructor(parcelIn: Parcel) : this ( […]

如何用kotlin标记List

我想要通过Bundle传递一个数据类(包含int作为一个属性列表)到其他活动,因此我需要添加Parcelable实现到我的数据类。 任何想法如何包裹这个属性? data class Test(val id: Long, val files: List?) : Parcelable { constructor(parcel: Parcel) : this( parcel.readLong(), TODO(“files”)) override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeLong(id) } override fun describeContents(): Int { return 0 } companion object CREATOR : Parcelable.Creator { override fun createFromParcel(parcel: Parcel): Test { return Test(parcel) } override fun newArray(size: Int): […]

在kotlin中写入可分配的值

后端返回可空Int? 我应该如何编写可以为空的值? date class Foo (var value: Int?){ constructor(source: Parcel) : this( source.readInt() ) override fun writeToParcel(dest: Parcel, flags: Int) { dest.writeInt(value) // doesn’t compile – writeInt expect no null value } } 现在我解决了: dest.writeInt(value?: -1) 然后检查到-1 或者像Int字符串写入Int然后使用…的值… 但我认为这是丑陋的错误。 解决! 我的答案: source.readValue(Int::class.java.classLoader) as Int?, dest.writeValue(value)