Jay Garzon
Jay Garzon

Reputation: 41

Changing textview inside Cardview

I am stuck in my android project and I hope someone would give time to check it.

I have a recyclerview populated by cardviews. Each cardview has Textview in it.

What I am trying to accomplish is for each cardview, i want to change their textview.text based on the button clicked.

for(card in cardList){
   val textView = card.findViewWithTag("TEXT_VIEW") as TextView
   //get button click
   //change text
}

EDITED: I have this following code:

for(card in cardList){
   //get the textview inside
  card.findViewWithTag("TEXT_VIEW") as TextView
  //set onclick listener for all buttons
  for(button in buttons){
    button.setOnClickListener{
        tv.text = button.text
   }
}

But what happening is if I click a button, all textviews within the cardviews will change to the button.text

What I wanted to do is if I click a button, It will only change the textview of the first cardview, then click a button again and change the textview of the second cardview's textview and soon.

Please do refer to the image attached. Thank you and I hope that you're all doing great this pandemic.

number-sorter-app image

Upvotes: 0

Views: 151

Answers (1)

damienG
damienG

Reputation: 444

I don't know what you want to do exactly, but maybe you mean something like that:

Make variable which will said which index of cardList texview should be filled:

val textViewIndexToFill = 0

And then when you want fill textview one by one you should do something like that (based on yours code):

var txtView = cardList[textViewIndexToFill].findViewWithTag("TEXT_VIEW") as TextView
for(button in buttons) {
button.setOnClickListener{
   txtView.text = button.text
   textViewIndexToFill++
}

Upvotes: 0

Related Questions