ArrayList<Any>にparcelableを実装する方法が分かりません。
作成したリストの中にはString型と独自に作成したクラス(UploadedItem)が入っています。
String型かUploadedItem型から複数のデータをリストに格納して画面遷移したいのですがうまくできません。
下記のコードはParcelable Code Generator(for kotlin)を使って作成しました。
kotlin
1class Images : Parcelable { 2 var selectedImages: ArrayList<Any> = ArrayList() 3 4 override fun toString(): String { 5 return "Images(selectedImages=$selectedImages)" 6 } 7 8 companion object { 9 @JvmField val CREATOR: Parcelable.Creator<Images> = object : Parcelable.Creator<Images> { 10 override fun createFromParcel(source: Parcel): Images = Images(source) 11 override fun newArray(size: Int): Array<Images?> = arrayOfNulls(size) 12 } 13 } 14 15 constructor(source: Parcel) : this( 16 //ここに何を記述すればいいか分かりません 17 //No value passed for parameter source(パラメータソースに渡された値はありません)と表示されています。 18 ) 19 20 override fun describeContents() = 0 21 22 override fun writeToParcel(dest: Parcel, flags: Int) {} 23} 24 25class UploadedItem : Parcelable { 26 27 override fun equals(obj: Any?): Boolean { 28 return obj is UploadedItem && obj.hashCode() == this.hashCode() 29 } 30 31 override fun hashCode(): Int { 32 return if (localFile != null) localFile!!.absolutePath.hashCode() else uploadId.toInt() 33 } 34 35 val extension: String 36 get() { 37 if (this.type != null) { 38 if (this.type!!.contains("uploadItem")) { 39 if (this.type!!.contains("jpeg")) { 40 return "jpg" 41 } else if (this.type!!.contains("gif")) { 42 return "gif" 43 } else if (this.type!!.contains("png")) { 44 return "png" 45 } 46 } 47 } 48 return "jpg" 49 } 50 51 var uploadId: Long = 0 52 var thumbUrl: String? = null 53 var imageUrl: String? = null 54 var originalUrl: String? = null 55 var type: String? = null 56 var size: Int = 0 57 var width: Int = 0 58 var height: Int = 0 59 var latitude: Double = 0.toDouble() 60 var longitude: Double = 0.toDouble() 61 var altitude: String? = null 62 var comment: String? = null 63 var takenTime: String? = null 64 var uploadTime: String? = null 65 var uploader: User? = null 66 var localFile: File? = null 67 68 constructor(localFile: File) { 69 this.localFile = localFile 70 } 71 72 override fun toString(): String { 73 return "UploadedItem{" + 74 "uploadId=" + uploadId + 75 ", thumbUrl='" + thumbUrl + '\'' + 76 ", imageUrl='" + imageUrl + '\'' + 77 ", originalUrl='" + originalUrl + '\'' + 78 ", type='" + type + '\'' + 79 ", size=" + size + 80 ", width=" + width + 81 ", height=" + height + 82 ", latitude=" + latitude + 83 ", longitude=" + longitude + 84 ", altitude='" + altitude + '\'' + 85 ", comment='" + comment + '\'' + 86 ", takenTime='" + takenTime + '\'' + 87 ", uploadTime='" + uploadTime + '\'' + 88 ", uploader=" + uploader + 89 ", localFile=" + localFile + 90 '}' 91 } 92 93 constructor() {} 94 95 override fun describeContents(): Int { 96 return 0 97 } 98 99 override fun writeToParcel(dest: Parcel, flags: Int) { 100 dest.writeLong(this.uploadId) 101 dest.writeString(this.thumbUrl) 102 dest.writeString(this.imageUrl) 103 dest.writeString(this.originalUrl) 104 dest.writeString(this.type) 105 dest.writeInt(this.size) 106 dest.writeInt(this.width) 107 dest.writeInt(this.height) 108 dest.writeDouble(this.latitude) 109 dest.writeDouble(this.longitude) 110 dest.writeString(this.altitude) 111 dest.writeString(this.comment) 112 dest.writeString(this.takenTime) 113 dest.writeString(this.uploadTime) 114 dest.writeParcelable(this.uploader, flags) 115 dest.writeSerializable(this.localFile) 116 } 117 118 protected constructor(`in`: Parcel) { 119 this.uploadId = `in`.readLong() 120 this.thumbUrl = `in`.readString() 121 this.imageUrl = `in`.readString() 122 this.originalUrl = `in`.readString() 123 this.type = `in`.readString() 124 this.size = `in`.readInt() 125 this.width = `in`.readInt() 126 this.height = `in`.readInt() 127 this.latitude = `in`.readDouble() 128 this.longitude = `in`.readDouble() 129 this.altitude = `in`.readString() 130 this.comment = `in`.readString() 131 this.takenTime = `in`.readString() 132 this.uploadTime = `in`.readString() 133 this.uploader = `in`.readParcelable<User>(User::class.java.getClassLoader()) 134 this.localFile = `in`.readSerializable() as? File 135 } 136 137 companion object { 138 139 val TAG = "Upload" 140 141 @JvmField 142 val CREATOR: Parcelable.Creator<UploadedItem> = object : Parcelable.Creator<UploadedItem> { 143 override fun createFromParcel(source: Parcel): UploadedItem { 144 return UploadedItem(source) 145 } 146 147 override fun newArray(size: Int): Array<UploadedItem?> { 148 return arrayOfNulls(size) 149 } 150 } 151 } 152}
constructorに何を記述すべきなのか教えてください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/22 00:15