6

I use the getString() to become String from string.xml. In my class (non-activity) does not work:

  • context.getResources().getString()
  • getResources().getString()
  • context.getResources().getString()

How do I get the String to this class?

public class myClass{
     public String[] myInfo(String ID) {
        String myString = getRessources().getString(R.string.myString);
     };
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1878413
  • 1,813
  • 4
  • 18
  • 24
  • your class does not extend activity. you need to pass the context to get resources – Raghunandan May 26 '13 at 13:49
  • 1
    Follow the answer below. As mentioned in the answer you need the activity context to get resources. pass it to the constructor of the non-activity class and use the same. – Raghunandan May 26 '13 at 14:06

1 Answers1

14

You have to call context.getResources().getString(), but you have to pass in a context in order to do that.

You can create a constructor, that takes that parameter for example:

Context context;

public myClass(Context context) {
    this.context = context;
}
Ahmad
  • 69,608
  • 17
  • 111
  • 137