본문 바로가기

안드로이드

[Android Studio] ParselableArrayList 로 Intent 하는 법! (Kotlin)

728x90

 

 

데이터 Dto 생성 . 

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

@Parcelize
data class MemberDto(
    val userId: String,
    val nickname: String,
    val profileImage: String
) : Parcelable {
    override fun toString(): String {
        return "userId:$userId, nickname:$nickname, profileImage:$profileImage"
    }
}

 

 

 

데이터 Set

val list = ArrayList<MemberDto>()
uiModel.members.forEach {
    list.add(MemberDto(it.userId,it.nickname,it.plainProfileImageUrl))
}

 

 

MainActivity Intent 

val intent = Intent(this@ChatMessageActivity, AnotherActivity::class.java).apply {
    putParcelableArrayListExtra(MEMBER_DTO, list)
}

 

 

AnotherActivity

intent.getSerivalizableExtra() 를 사용해서 받는다. 

 

intent?.let { it ->
    val list = it.getSerializableExtra(MEMBER_DTO) as ArrayList<MemberDto>
}

 

 

사용법 

for(member in list) {
	println("$userId")
}

 

 

 

번외 .  Data 를 넘길 때 

 

intent.putExtra("member", MEMBER_DTO("userId", "nickname", "image"))

 

데이터를 받을 때 

var user = intent.getParcelableExtra<MEMBER_DTO>("member")
tvText.text = user.toString()