1

I want to pass arrayList of objects type between 2 activities.
By one of 2 ways :
either : By intent (I do not find put or get extra) type of arrayList !)
or : By OOP . By a class using set & get functions . But the result is still null
(I do not know how to make static variables in kotlin class) .


My array list :

var listSongs=ArrayList<songInfo>()

Inside songInfo class :

var title:String?=null
var authorName:String?=null
var songURL:String?=null

Passing class : "passing_class"

What you prefer any way ?

N L
  • 13
  • 1
  • 5

2 Answers2

2

First of all, you have to make sure SongInfo has implemented Parcelable like code below

data class SongInfo(
        var title: String? = null,
        var authorName: String? = null,
        var songURL: String? = null
) : Parcelable {

    constructor(parcel: Parcel) : this(
            parcel.readString(),
            parcel.readString(),
            parcel.readString())

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(title)
        parcel.writeString(authorName)
        parcel.writeString(songURL)
    }

    override fun describeContents(): Int {
        return 0
    }

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

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

After that, the code in the first activity should be similar to this

class ActivityOne: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        val button = Button(this) // Just for an example

        val arrayList = arrayListOf(
                SongInfo("Love yourself", "Justin Bieber", "https://example.com"),
                SongInfo("Love yourself", "Justin Bieber", "https://example.com"),
                SongInfo("Love yourself", "Justin Bieber", "https://example.com"),
                SongInfo("Love yourself", "Justin Bieber", "https://example.com")
        )

        button.setOnClickListener {
            val intent = Intent(applicationContext, ActivityTwo::class.java)
            intent.putExtra("songs", arrayList)
            startActivity(intent)
        }
    }
}

And then in order to receive your array list from the first activity you need to parse it by using getParcelableArrayListExtra<SongInfo>(..) method.



class ActivityTwo: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val songs = intent?.getParcelableArrayListExtra<SongInfo>("songs") ?:
                throw IllegalStateException("Songs array list is null")

        println(songs) // There you go
    }
}

Android Studio could help you create a boilerplate code of Parcelable as well by Option + return on Mac.

Seanghay
  • 1,076
  • 1
  • 11
  • 24
0

1.) Create songInfo model class as parcelable.

2.) Pass this parcelbale model class in intent and use Parcelable.putExtra() method using key.

3.) Get Parcelable arraylist into another class by Parcelable.getExtras() using valid key.

Pawan Soni
  • 860
  • 8
  • 19