Kotlin:使用putExtra和Parcelable进行数据传递

问题描述 投票:0回答:1

我正在尝试使用putExtra函数传递数据。下图描述了我的目标。

enter image description here

我在这样的First Activity中设置了一个按钮舔监听器,而没有putExtra;

        downloadButton?.setOnClickListener {
            val toSendProjectListIntent = Intent(this, LocalProjectListActivity::class.java)

            toSendProjectListIntent.putExtra("download", downloaded)
            startActivity(toSendProjectListIntent)
        }

第二活动中的RescyclerView是;

        val projectList = ArrayList<ProjectListDataModel>()

        val downloadedProjectList = intent.getParcelableArrayListExtra<ProjectListDataModel>("download")

        localProjectAdapter = LocalProjectsRecyclerAdapter(projectList, this)
        projectListView.adapter = localProjectAdapter
        projectListView.setHasFixedSize(true)
        projectListView.layoutManager = LinearLayoutManager(this)
    }

问题是,打开应用程序时,我将val projectList = ArrayList<ProjectListDataModel>设置为空白屏幕。但是,我不知道如何将所选列表放入localProjectAdapter。一种情况是,当我们单击下载按钮时,不应更改该活动。应保持在“主要”活动中。

而且,您认为这是正确的方法吗?我不确定使用putExtra和parcelable函数的选择:(

任何想法?

android kotlin parcelable
1个回答
0
投票

您必须向Kotlin提供有关对象列表类型的更多信息。试试这个:

val downloadedProjectList  : ArrayList<ProjectListDataModel> =  intent.getParcelableArrayListExtra("download")

现在,您可以将downloadedProjectList放入适配器中

© www.soinside.com 2019 - 2024. All rights reserved.