I'm trying to set up this enum so that it has the ability to return the correct image, though I'm struggling with a way to incorporate the context since it is in a separate class.
public enum CubeT开发者_开发知识库ype
{
GREEN {
public Drawable getImage()
{
return Context.getResources().getDrawable( R.drawable.cube_green );
}
};
abstract public Drawable getImage();
}
The error I'm getting is:
Cannot make a static reference to the non-static method getResources() from the type Context
I guess you could have a context as a parameter to getImage():
...
GREEN {
public Drawable getImage(Context c)
{
return c.getResources().getDrawable( R.drawable.cube_green );
}
};
...
}
Why not just define the enum in a class that wraps the enum and a HashMap relating the enum values to resource id's?
精彩评论