0

I have next data class

data class LegendWrapper(
    val data: List<PlayerPerformance>? = emptyList()

)
data class PlayerPerformance(val name: String, val value: Int, val key: String)

data class TestList(val name: String, val data: LegendWrapper)

which I fill with data from Map

for((key, value ) in data.legends.all){
recList.add(TestList(key, value))
}

Further, by clicking on the name from the recyclerView, I want to transfer this list to another activity. How can I do it ?

i tried to do it like this

 heroesAdapt = HeroesRecyclerAdapter(this, recList){
   testList -> val heroesStatsActivity = Intent(this,HeroesStatsActivity::class.java)
   heroesStatsActivity.putExtra("nicknameHeroes", testList.name)
   heroesStatsActivity.putExtra("data", testList.data.toString())
   startActivity(heroesStatsActivity)
}

But I get on another screen just a line with which I cannot work

Ethernets
  • 201
  • 2
  • 12

2 Answers2

1

If you want to pass any data type other than primitive data, you should pass with putParcelable.

Follow these steps:

First, add the kotlin-android-extensions plugin to your build.gradle app.

build.gradle.kts

plugins {
    id("kotlin-android-extensions")
}

Next, make your data class Parcelable like this:

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class LegendWrapper(
    val data: List<PlayerPerformance>? = emptyList()
) : Parcelable

@Parcelize
data class PlayerPerformance(
    val name: String,
    val value: Int,
    val key: String
) : Parcelable

@Parcelize
data class TestList(
    val name: String,
    val data: LegendWrapper
) : Parcelable

And now you can put Parcelable into intent:

heroesAdapt = HeroesRecyclerAdapter(this, recList) { testList -> 
    val heroesStatsActivity = Intent(this, HeroesStatsActivity::class.java)
    heroesStatsActivity.putExtra("nicknameHeroes", testList.name)
    heroesStatsActivity.putExtra("data", testList.data)
    startActivity(heroesStatsActivity)
}

In the activity that receives the intent, you get the data as follows:

val nickNameHerors = intent.getStringExtra("nicknameHeroes")
val data = intent.getParcelableExtra<LegendWrapper>("data")
Dương Minh
  • 2,062
  • 1
  • 9
  • 21
  • I have no such function **putParcelabe** I was able to transfer simply **putExtra** But then when getting in the second activity when calling val test = intent? .Extras? .GetParcelable ("data") I get an error ***Not enough information to infer type variable T*** – Ethernets Aug 22 '21 at 15:10
0

use bundle.putParcelable

for receive data in other activity use : bundel.getParcelable

farid
  • 300
  • 1
  • 8
  • I still get a type mismatch error. Could you give more usage example? ```bandle.putParcelable("data", testList.data)``` Type mismatch. Required: Parcelable? Found: LegendWrapper – Ethernets Aug 22 '21 at 14:09