0
public class StorageStrings {

    public static String[] keysPrefs = {getString(R.string.key_mod), getString(R.string.key_vel),getString(R.string.key_num)};
    public static String[] defaultValues = {getString(R.string.default_value_mod),getString(R.string.default_value_vel),getString(R.string.default_value_num)};
}

I want to store my Strings in this class. But I cannot use getString() method to retrieve the String from the resource.

How can I get the String from resource in this self-defined class?

Thanks in advance

TrW236
  • 367
  • 1
  • 5
  • 14

2 Answers2

0

Unfortunately there is no way to do it without an android.content.Context object.

So you can either

  1. Pass a Context to the constructor of the self contained class; or
  2. Save the application context somewhere in a static variable that you can then access from your self contained class (see here and here)
Community
  • 1
  • 1
Jorge Galvão
  • 1,729
  • 1
  • 15
  • 28
0

If the function is in any Activity then you can use following code:

public class StorageStrings {

    public static String[] keysPrefs = {getString(R.string.key_mod), getString(R.string.key_vel),getString(R.string.key_num)};
    public static String[] defaultValues = {getString(R.string.default_value_mod),getResources().getString(R.string.default_value_vel),getResources().getString(R.string.default_value_num)};
}

If the function is not in any activity then you will need context of that activity and can use following code

context.getResources().getString(R.string.default_value_num)

instead of

getResources().getString(R.string.default_value_num)};
Sonam
  • 572
  • 4
  • 13