开发者

How would I set up this enum to return the image I want?

开发者 https://www.devze.com 2023-02-13 18:09 出处:网络
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.

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消