-2

I have a CardView in AndroidStudio with different ID's for each card. To search the different ID's i have made this:

for (int f = 0; f < mainGrid.getChildCount(); f++) {
    if (f == count) {
        int index = f;
        String id = "food"+index;

        foodName =  findViewById(R.id.id);
        foodName.getText();
    }
}

But at foodName = findViewById(R.id.id); says that "Cannot resolve symbol id"

There's any other way to do it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Marco
  • 33
  • 1
  • 1
  • 13

1 Answers1

2

No matter how many times you loop, if you do not have a view named id in your layout, you will get the error you are getting.

You cannot use a variable called id and then tag it onto R.id.id (which resolves as a numeric id value)

If you want to find a view when you know only its name, see this:

Find view by name

Kuffs
  • 35,581
  • 10
  • 79
  • 92