Search code examples
androidkotlinonclicklistener

how to create a new variable every time inside a for a loop in kotlin


I have a for loop that creates a cardView for every time it loops, then it uses setOnClickListener for every view with different extras , the problem is only the last loop parameters is being set to every created card, here is my code :

        for (finalItem1 in finalList1) {
            thePoints.clear()
            theSteps.clear()
            theIcons.clear()
            val step1 =
                "${getString(R.string.from_your_location)} \n ${getString(R.string.move_to)} \n $theStartStation"
            theSteps.add(step1)
            theIcons.add("ic_walk")
            thePoints.add(stationData[theStartStation]?.latitude.toString())
            thePoints.add(stationData[theStartStation]?.longitude.toString())
            val step2 =
                "${getString(R.string.from)} : $theStartStation \n ${getString(R.string.take)} : ${finalItem1.child("transportation_type").value} \n ${finalItem1.child("notes").value} \n ${finalItem1.child("price").value} EGP \n ${getString(R.string.to)} : $theEndStation"
            theSteps.add(step2)
            theIcons.add("ic_bus")
            thePoints.add(stationData[theEndStation]?.latitude.toString())
            thePoints.add(stationData[theEndStation]?.longitude.toString())
            val cardView = CardView(this)
            val cardLinearLayout = LinearLayout(this)
            cardLinearLayout.orientation = LinearLayout.VERTICAL
            val params = RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            params.setMargins(16, 16, 16, 16)
            cardView.radius = 15f
            cardView.setCardBackgroundColor(Color.parseColor("#64b0e3"))
            cardView.setContentPadding(36, 36, 36, 36)
            cardView.layoutParams = params
            cardView.cardElevation = 30f
            val quote = TextView(this)
            quote.text = "${getString(R.string.from)} $theStartStation \n ${finalItem1.child("notes").value} \n ${finalItem1.child("price").value} EGP"
            cardLinearLayout.addView(quote)
            cardView.addView(cardLinearLayout)
            optionsLayout.addView(cardView)
            cardView.setOnClickListener {
                val tripIntent =
                    Intent(this, NavigationActivity::class.java)
                tripIntent.putStringArrayListExtra("theSteps",
                    theSteps as java.util.ArrayList<String>?
                )
                tripIntent.putStringArrayListExtra("theIcons",
                    theIcons as java.util.ArrayList<String>?
                )
                tripIntent.putStringArrayListExtra("thePoints",
                    thePoints as java.util.ArrayList<String>?
                )
                tripIntent.putExtra("fromLat", fromLocation.latitude)
                tripIntent.putExtra("fromLon", fromLocation.longitude)
                tripIntent.putExtra("toLat", toLocation.latitude)
                tripIntent.putExtra("toLon", toLocation.longitude)
                startActivity(tripIntent)
            }

the problem lies here : val cardView = CardView(this)

as it created the same listener for every card cardView.setOnClickListener


Solution

  • Every listener instance references theSteps, theIcons and theIcons objects which are declared outside the loop. You clear these lists and then add items to them in every loop iteration, so only values from last iteration are actually used. Just declare these lists inside a loop.