【问题标题】:Android Kotlin passing arrayList<object> to other activityAndroid Kotlin 将 arrayList<object> 传递给其他活动
【发布时间】:2020-10-02 00:37:15
【问题描述】:

我想在 2 个活动之间传递对象类型的数组列表。
通过以下两种方式之一:
要么:有意(我没有找到 put 或 get extra)类型的 arrayList !)
或: 通过 OOP。通过使用 set & get 函数的类。但结果仍然为空
(我不知道如何在 kotlin 类中制作静态变量)。


我的数组列表:

var listSongs=ArrayList<songInfo>()

songInfo 类内部:

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

通过类:“passing_class”

你更喜欢哪种方式?

【问题讨论】:

  • intent 有什么问题?你做了什么结果是空的?向我们展示代码。
  • @AnimeshSahuI 我找不到 -put 或 get extry- 类型的 arrayList !

标签: android android-studio oop kotlin arraylist


【解决方案1】:

首先,您必须确保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)
        }
    }
}

之后,第一个活动中的代码应该是这样的

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)
        }
    }
}

然后为了从第一个活动中接收您的数组列表,您需要使用getParcelableArrayListExtra&lt;SongInfo&gt;(..) 方法对其进行解析。



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 可以帮助您创建 Parcelable 的样板代码 以及在 Mac 上由 Option + return 提供。

【讨论】:

    【解决方案2】:

    1.) 将 songInfo 模型类创建为 parcelable。

    2.) 在意图中传递这个 parcelbale 模型类,并使用键使用 Parcelable.putExtra() 方法。

    3.) 使用有效键通过 Parcelable.getExtras() 将 Parcelable arraylist 获取到另一个类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      相关资源
      最近更新 更多