What is the best way to deal with adding Drawable images programmatically?

Viewed 27

I'm working on an app that deals with playing cards. Right now, I have a folder, drawable/cards that has dozens of files, numbered sequentially from 00.png, that are images of the card faces.

I also have two classes, Deck and Card. Cards have a reference to the Drawable image of their face. Deck has a list of Cards that I would like to construct within a for loop. This all looks like,

public class Card{

    public Card(Drawable image) {}

};
public class Deck{

    List<Card> cardList;

    public Deck(){
        for(int i=0; i<52; ++i){
            cardList.add( new Card( ??? ) );
        }
    }

}

My question is, is there any way for me to generate references to the Drawable images of the card faces when constructing the Cards this way? Or do I need to find a different approach?

1 Answers

Have you already tried handing over the resource id of your drawable image instead of the whole drawable itself? Then you should be able to use this to draw the image in your Card-class using the setImageResource() function on your imageView.

Related