0

i been trying to send up an array of string using parcel in kotlin, i tried several things like converting to array before using writeTypedArray, or directly using writeStringArray. I also tried to read using createTypedArray and createdStringArray.

For most of the case, the content i receive is right ( i mean the inside of the array does not change ), but there is some case where it becomes a nulls and some link in the array.

I have checked my code, and print it before and after the usage of intent, and thats where the difference is, so i guess the fault lies in the parcel

This is the place where i put my intent, when i call showGenres(), its show the exact genres like Comedy and Slice of Life

val anime : Anime = news.anime
        val intent : Intent = Intent(holder.btnViewMore.context, DetailActivity::class.java)
        news.anime.showGenres()
        intent.putExtra("choosen", anime)
        holder.btnViewMore.context.startActivity(intent)

but, after i receive the intent, when i call the showGenres, the content become nulls (most of them) and some string like "P��https://s4.anilist.co/file/anilistcdn/media/anime/banner/104199-ha1rxhhOb8Yp.jpg��������Comedy������Slice".

anime = intent.getParcelableExtra("choosen")
    anime.showGenres()

enter image description here

Below are my class which use parcellable

package com.example.jnimekuy.Model

import android.os.Parcel
import android.os.Parcelable
import android.util.Log

class Anime(
    var id: Int = 0,
    var title: String? = "",
    var image: String? = "",
    var description: String? = "",
    var status: String? = "",
    var startDate: String? = "",
    var endDate: String? = "",
    var season: String? = "",
    var episode: Int? = 1,
    var duration: Int? = 1,
    var trailer: String? = "",
    var cover: String? = "",
    var banner: String? = "",
    var genreList: List<String>? = ArrayList(),
    var avgScore: Float? = 1F
):Parcelable{
    constructor(parcel: Parcel) : this(
        parcel.readInt(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString()?.replace("<br>","",true),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readInt(),
        parcel.readInt(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.createStringArrayList()?.filterNotNull(),
        parcel.readFloat()
    )

override fun writeToParcel(parcel: Parcel, p1: Int) {
    parcel.writeInt(id)
    parcel.writeString(title)
    parcel.writeString(image)
    parcel.writeString(description)
    parcel.writeString(status)
    parcel.writeString(startDate)
    parcel.writeString(endDate)
    parcel.writeString(season)
    episode?.let { parcel.writeInt(it) }
    duration?.let { parcel.writeInt(it) }
    parcel.writeString(trailer)
    parcel.writeString(cover)
    parcel.writeString(banner)
    parcel.writeStringList(genreList)
    avgScore?.let { parcel.writeFloat(it) }
}

fun showGenres(){
    for(i in this!!.genreList!!)
    {
        Log.d("genre",i)
    }
}

override fun describeContents(): Int {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

companion object CREATOR : Parcelable.Creator<Anime> {
    override fun createFromParcel(parcel: Parcel): Anime {
        return Anime(parcel)
    }

    override fun newArray(size: Int): Array<Anime?> {
        return arrayOfNulls(size)
    }
}

}

DKVV
  • 23
  • 1
  • 4

1 Answers1

0

Ok, i found the problem, because i use let , which is if not null then send something, then if its null, it does not send anything, but still get opened by parcel constructor. There is nothing wrong with the write and type string array. what i did was to put if null then still send something

    parcel.writeInt(id)
    parcel.writeString(title)
    parcel.writeString(image)
    parcel.writeString(description)
    parcel.writeString(status)
    parcel.writeString(startDate)
    parcel.writeString(endDate)
    parcel.writeString(season)
    if(episode != null){ parcel.writeInt(episode!!) }
    else { parcel.writeInt(0) }
    if(duration != null){ parcel.writeInt(duration!!) }
    else { parcel.writeInt(0) }
    parcel.writeString(trailer)
    parcel.writeString(cover)
    parcel.writeString(banner)
    parcel.writeStringList(genreList)
    avgScore?.let { parcel.writeFloat(it)
DKVV
  • 23
  • 1
  • 4