1

I am trying to set an image based on the enum. I did try to do some research on how to do that using this post : How to match int to enum but wasn't really sure how to actually use it according to my code.

Here's the enum:

public enum ImageValue{

        Image1(1,R.drawable.clubs1),
        Image2(2,R.drawable.hearts),
        Image3(3,R.drawable.diamonds),
        Image4(4,R.drawable.spades);

        private int imagevalue;
        private int image;
        private ImageValue(int value, int drawable){

            this.imagevalue = value;
            this.image = drawable;
        }

        public int getImageValue(){
            return image;
        }

    }

This is where I am getting the image values and other values:

public void NewDeck() {

        deck = new ArrayList<Card>();

        for (int i = 0; i < 13; i++) {
            value = CardValue.values()[i];

            for (int j = 0; j < 4; j++) {
                card = new Card(value, Suit.values()[j], ImageValue.values()[j] );
                this.deck.add(card);
            }
        }

        Collections.shuffle(deck);

        Iterator<Card> cardIterator = deck.iterator();

        System.out.println(deck.size());

        while (cardIterator.hasNext()) {
            aCard = cardIterator.next();


        }
        System.out.println("One Card Value" + "--" + aCard.getCardValue()
                + " of " + aCard.getSuit()+"--"+"Image Value" +"----" + aCard.getImagevalue());
    }

Here's one solution from the post I mentioned:

**coinView.setImageResource(coinArray[x].getImage());**

Now, On click of a button I want to set that random image that's being generated to one of my image views.

Here:

public void onClick(View v) {



        randomImage.setImageResource(**Want to put the image here!**);


    }

I am a bit new to Enum concept and not sure how exactly to use it.

A little help will be really appreciated..Thank's in advance.

Community
  • 1
  • 1
mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • Actually, I figured out the solution..If someone knows a better way to do it, they are most welcome, I am doing it this way: randomImage.setImageResource(aCard.getImagevalue().getImageValue()); – mike20132013 Mar 10 '14 at 06:10

0 Answers0