简体   繁体   English

Android Kotlin 通过 arrayList<object> 到其他活动<div id="text_translate"><p>我想在 2 个活动之间传递对象类型的 arrayList。<br> 通过以下两种方式之一:<br> 要么:有意(我没有找到放置或获得额外)类型的 arrayList !)<br> 或:OOP。 由 class 使用设置和获取功能。 但是结果还是null<br> (我不知道如何在 kotlin 类中制作 static 变量)。</p><hr><p> 我的数组列表:</p><pre> var listSongs=ArrayList&lt;songInfo&gt;()</pre><p> 里面的 songInfo class:</p><pre> var title:String?=null var authorName:String?=null var songURL:String?=null</pre><p> 通过 class:“ <strong>passing_class</strong> ”</p><p> 你喜欢什么方式?</p></div></object>

[英]Android Kotlin passing arrayList<object> to other activity

I want to pass arrayList of objects type between 2 activities.我想在 2 个活动之间传递对象类型的 arrayList。
By one of 2 ways:通过以下两种方式之一:
either: By intent (I do not find put or get extra) type of arrayList !)要么:有意(我没有找到放置或获得额外)类型的 arrayList !)
or: By OOP.或:OOP。 By a class using set & get functions.由 class 使用设置和获取功能。 But the result is still null但是结果还是null
(I do not know how to make static variables in kotlin class). (我不知道如何在 kotlin 类中制作 static 变量)。


My array list:我的数组列表:

var listSongs=ArrayList<songInfo>()

Inside songInfo class:里面的 songInfo class:

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

Passing class: " passing_class "通过 class:“ passing_class

What you prefer any way?你喜欢什么方式?

First of all, you have to make sure SongInfo has implemented Parcelable like code below首先,您必须确保SongInfo已实现Parcelable ,如下面的代码

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.然后为了从第一个活动接收您的数组列表,您需要使用getParcelableArrayListExtra<SongInfo>(..)方法对其进行解析。



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. Android Studio 可以帮助您创建 Parcelable 的样板代码,也可以通过 Mac 上的Option + return创建。

1.) Create songInfo model class as parcelable. 1.) 创建 songInfo model class 为 parcelable。

2.) Pass this parcelbale model class in intent and use Parcelable.putExtra() method using key. 2.) 在意图中传递这个parcelbale model class 并使用 Parcelable.putExtra() 方法使用密钥。

3.) Get Parcelable arraylist into another class by Parcelable.getExtras() using valid key. 3.) 使用有效密钥通过 Parcelable.getExtras() 将 Parcelable arraylist 放入另一个 class 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM