Issue
I have a stringDef/ TypeDef class as follows
public class Codes {
public static final String Code_1 = "Code1";
public static final String Code_2 = "Code2";
public static final String Code_3 = "Code3";
@Retention(RetentionPolicy.SOURCE) @StringDef({
Code_1, Code_2, Code_3 })
public @interface CodesMessageDef {
}
}
I would like to set values of Code_1,2,3 from R.String.code_1 rather than manually entering.
Is there any way to achieve this usecase.
Thanks in advance.........
Solution
You can read this answer for how to get application context from a static method. Note that using this to get the context is not a memory leak because you are using an application context.
Now you can do:
public static final String Code_1 = MyApplication.getAppContext().getString(R.id.code1);
public static final String Code_2 = MyApplication.getAppContext().getString(R.id.code2);
public static final String Code_3 = MyApplication.getAppContext().getString(R.id.code3);
I guess it should work.
Answered By - Nicolas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.