skip to Main Content

I have this code in activity A :

private fun randomImage() {
        val number: Int = (1..4).random()
        if (!selectedImageList.contains(number)) selectedImageList.add(number)
        if (selectedImageList.size == 2) return
        else randomImage()
    }

and have this code in activity B :

     private fun setImages(binding: ActivityP1Binding) {

        selectedImageList.forEachIndexed { index, number ->

            when (number) {
                1 -> {
                    if (index == 0) binding.a1.setImageResource(R.drawable.Omo1)
                    if (index == 1) binding.b1.setImageResource(R.drawable.Omo1)
                }
                2 -> {
                    if (index == 0) binding.a1.setImageResource(R.drawable.Omo2)
                    if (index == 1) binding.b1.setImageResource(R.drawable.Omo2)
                }
                3 -> {
                    if (index == 0) binding.a1.setImageResource(R.drawable.Omo3)
                    if (index == 1) binding.b1.setImageResource(R.drawable.Omo3)
                }
                4 -> {
                    if (index == 0) binding.a1.setImageResource(R.drawable.Omo4)
                    if (index == 1) binding.b1.setImageResource(R.drawable.Omo4)
                }

So It needs codes to send selectedImageList ( included index and number ) from activity A . And codes to receive in activity B . I used some Codes to intent by putParcelableArrayListExtra , but they did not work .
Thanks

2

Answers


  1. Sounds like all you need to do is send an array of integers from activity A to activity B. This can be done with putExtra (https://developer.android.com/reference/kotlin/android/content/Intent#putextra_17) and getIntArrayExtra (https://developer.android.com/reference/kotlin/android/content/Intent#getintarrayextra)

    See https://www.techiedelight.com/convert-list-to-array-kotlin/ for converting a List to an Array

    Login or Signup to reply.
  2. Try in this way it will definitely work
    So in activity A, add this code in intent part

    intent.putExtra("tag",ArrayList(selectedImageList))
    

    and in Activity B,

    val my_List=intent.getStringArrayListExtra("tag") as ArrayList
    

    and you will receive a ArrayList of your values in Activity B

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search