Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
     };
}
like image 857
user1878413 Avatar asked May 26 '13 13:05

user1878413


1 Answers

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;
}
like image 196
Ahmad Avatar answered Oct 20 '22 02:10

Ahmad