Static Access To An Android App's Resources?
I have a problem re. an Android application's resources: My application has misc. modes (edit/plan/exec), which I would like to describe using an enumeration type. I would, however
Solution 1:
enumMode {
EDIT (R.string.mode_edit),
PLAN (R.string.mode_plan),
EXEC (R.string.mode_exec);
String id;
Mode(String id) { this.id = id; }
publicStringgetName(Resources r){ return r.getText(id); }
@OverridepublicStringtoString() { returnthis.name; }
}
Alternatively you can do following:
publicclassClassName {
publicstatic Resources res;
}
In your Application.onCreate()
or Activity.onCreate()
:
ClassName.res = getResources();
and in your enum Mode
:
@OverridepublicStringtoString() {
Resources res = ClassName.res;
if (res==null){ returnsuper.toString(); }
else { return res.getText(id); }
}
Solution 2:
Thanks, radek-k, for the examples! In the meantime I came up myself with a somewhat similar idea, namely I added a static method to the enum to which I then pass the resource-handle during the Activity's onCreate()-method. That allows the toString()-method then to access the resource strings. IMHO not very elegant, but it works...
Cheers, Michael
Solution 3:
Based on this https://stackoverflow.com/a/3560656/262462 and because R.string contains integers
enum Mode {
EDIT (R.string.mode_edit),
PLAN (R.string.mode_plan),
EXEC (R.string.mode_exec);
int id;
Mode(int id) { this.id = id; }
public String toString(Resources r) { return r.getString(id); }
}
Post a Comment for "Static Access To An Android App's Resources?"