Android using Drawables in Enum

I have a project that I have to create an array of coins and work with it. My GUI looks like this: http://i.imgur.com/eRzN3Sb.png

I want to be able to load the corresponding image from coinArray for each coin. basically, I want to say that coinView.setBackgroundResource (coinArray [x] .image) I suppose I need to use a drawing object in some way, and I was hoping it could be included in my enum class. the enum class looks like

public enum Currency { Penny(1), Nickel(5), Dime(10), Quarter(25); private int value; private Currency(int value) { this.value = value; } } 

Each coin in the array has a currency value, so I can calculate them. I would like to add an attractive or some other object that will allow me to convey the correct image for each coin.

thanks

+3
android enums drawable
source share
1 answer
 public enum Currency { Penny(1,R.drawable.xxx), Nickel(5,R.drawable.yyy),...; private int value; private int image private Currency(int value,int drawableId) { this.value = value; this.image=drawableId; } public int getImage(){ return image; } } 

There are many ways to do this. This is one of them. to use him:

 coinView.setImageResource(coinArray[x].getImage()); 
+7
source share

All Articles