从枚举类中获取名称

时间:2013-03-03 09:59:23

标签: android enums drawable

我有这样的枚举类:

public enum Names {

Logo1(R.drawable.adese,"Adese"),
Logo2(R.drawable.afra,"Afra");

private int resource;
private String description;
private Names(int resource, String description){
    this.description = description;
}

public boolean isCorrect(String input){
return input.equals(description);
}
}

我希望从活动的drawable中获取枚举的名称。换句话说,当我向某些方法提供R.drawable.adese时,我想获得Logo1。当我提供R.drawable.afra时,我想获得Logo2等等。我怎么能这样做?

在我的活动中

Names name;
.
.
.
id = R.drawable.adese;
name = Names.forDrawable(id);
    check.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            a=name.isCorrect(text.getText().toString());
            if(a==true){
            Intent intent = new Intent(LogoActivity.this, LevelSelectionActivity.class);
            startActivity(intent);
        }
        }
        });

检查按钮等在活动中声明,我没有写在这里缩短。

我收到了错误消息:

03-03 12:16:27.125: E/AndroidRuntime(6091): FATAL EXCEPTION: main
03-03 12:16:27.125: E/AndroidRuntime(6091): java.lang.NullPointerException
03-03 12:16:27.125: E/AndroidRuntime(6091):     at         com.example.turkishlogoquiz.LogoActivity$1.onClick(LogoActivity.java:57)
03-03 12:16:27.125: E/AndroidRuntime(6091):     at android.view.View.performClick(View.java:4223)
03-03 12:16:27.125: E/AndroidRuntime(6091):     at android.view.View$PerformClick.run(View.java:17275)
03-03 12:16:27.125: E/AndroidRuntime(6091):     at android.os.Handler.handleCallback(Handler.java:615)
03-03 12:16:27.125: E/AndroidRuntime(6091):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 12:16:27.125: E/AndroidRuntime(6091):     at android.os.Looper.loop(Looper.java:137)
-03 12:16:27.125: E/AndroidRuntime(6091):   at android.app.ActivityThread.main(ActivityThread.java:4898)
03-03 12:16:27.125: E/AndroidRuntime(6091):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 12:16:27.125: E/AndroidRuntime(6091):     at java.lang.reflect.Method.invoke(Method.java:511)
03-03 12:16:27.125: E/AndroidRuntime(6091):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-03 12:16:27.125: E/AndroidRuntime(6091):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-03 12:16:27.125: E/AndroidRuntime(6091):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

只需创建一个静态方法,该方法将为可绘制资源返回Names值。

public enum Names {
    Logo1(R.drawable.adese,"Adese"),
    Logo2(R.drawable.afra,"Afra");

    private int resource;
    private String description;

    private Names(int resource, String description) {
        this.resource = resource;
        this.description = description;         
    }

    public boolean isCorrect(String input){
        return input.equals(description);
    }

    public static Names forDrawable(int resourceId) {
        for (final Names name : Names.values()) {
            if (name.resource == resourceId) {
                return name;
            }
        }

        return null;
    }
}