0

I following the answer in this topic Gridview with two columns and auto resized images

But, I get error at this line

items.add(new Item("Red", R.drawable.red));

The cause is the requirement of 2nd params is a String. But, R.drawable.red is integer.

So, anyone can teach me solve this problem?

Thanks

Community
  • 1
  • 1
Anon
  • 39
  • 1
  • 7

4 Answers4

1

You need to create your custom Item class, which has the following constructor.

class Item {
    Item(String str, int id) {
    ..
    }
}
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • Thanks for your answer. sorry all because this stupid question, I come back to programming for a long time not touching – Anon Aug 29 '14 at 10:27
  • Don't apologize. Happens to all of us. Glad I could be of help. :) – Swayam Aug 29 '14 at 10:29
0

If 2nd param's type is String just convert the id to a string:

items.add(new Item("Red", String.valueOf(R.drawable.red)));

Later you can convert to integer with Integer.valueOf(str);

However you should change the class constructors parameter type to int as the other answer suggested. You wouldn't need to convert at all.

Simas
  • 43,548
  • 10
  • 88
  • 116
0

Sorry, but reviewing the topic you posted, the 2nd parameter in the Item class is int not String

sergiomse
  • 775
  • 12
  • 18
  • yes, in that topic the 2nd param is Int, but I use default Item in android the method add require 2nd param is String. I am trying to create a class Item as Swayam suggested – Anon Aug 29 '14 at 10:18
0

You want to change second param is string? In your link sample code MyAdapter Class have modal class Item you need to change like that.

private class Item {
        final String name;
        final String drawableId;

    Item(String name, String drawableId) {
        this.name = name;
        this.drawableId = drawableId;
    }
}
B M
  • 1,863
  • 1
  • 25
  • 40