0

I'm trying to pass data use putExtra function. My goal is described in the picture below. enter image description here

I set a button click listener with out putExtra in First Activity like this;

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

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

And the RescyclerView in Second Activity is;

        val projectList = ArrayList<ProjectListDataModel>()

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

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

Question is, I put val projectList = ArrayList<ProjectListDataModel> to set empty the Main activity, when I open the app. After that, I go the Second Activity and select the list by button click. However, I don't have idea, how to put the selected lists in the localProjectAdapter. One condition is, when we click the download button, the activity shouldn't be changed.. should be staying at the Second activity.

Moreover, Do you think this is right way? I'm not sure my choice to use the putExtra and parcelable function :(

Any idea?

Sean Kim
  • 25
  • 2
  • 9

1 Answers1

0

You have to give Kotlin more information about the type of your object list. Try this:

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

Now, you can put downloadedProjectList into your adapter

Usman Zafer
  • 1,261
  • 1
  • 10
  • 15
  • Thank you for your answer, however, it's not working...I set the adapter like this `localProjectAdapter = LocalProjectsRecyclerAdapter(downloadedProjectList, this)` But, got error like this "java.lang.IllegalStateException: intent.getParcelableArrayListExtra("download") must not be null" – Sean Kim Apr 01 '20 at 11:31
  • you also have to replace localProjectAdapter = LocalProjectsRecyclerAdapter(projectList, this) with localProjectAdapter = LocalProjectsRecyclerAdapter(downloadedProjectList, this) – Usman Zafer Apr 01 '20 at 11:34