The method getResources() and context is undefined for the type

user1878413 picture user1878413 · May 26, 2013 · Viewed 9.4k times · Source

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);
     };
}

Answer

Ahmad picture Ahmad · May 26, 2013

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;
}