Search code examples
androidandroid-contextgetstring

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


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

Solution

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