Search code examples
javaandroid

Android - Get the String value of R.string.x from a generic java class


I created an ArrayList of custom objects that I need for my App. Since the list is really long (ca. 350 elements) I decided to create a separate java class which has the createList and the getList method:

public ArrayList<obj> getList(){
    return obj;
}

which I need to get the list from my MainActivity.

My custom object looks like this:

new Obj(int[] images, String name)

I can't seem to be able to import the name from strings.xml since this class is not an Activity and does not have a context.

I tried with

Resources.getSystem().getString(R.string.c)

but I get a

ResourcesNotFound Exception

Is there a way to do it? on top of that I also need to concatenate more strings into one name.

I would really appreciate your help.


Solution

  • I can't seem to be able to import the name from strings.xml since this class is not an Activity and does not have a context.

    Provide a suitably-scoped Context to the Obj constructor. Or, provide the strings themselves to the Obj constructor, where the strings are resolved from their resource IDs by a Context.

    I tried with Resources.getSystem().getString(R.string.c) but I get a ResourcesNotFoundException

    That is because getSystem() returns a Resources for the JDK, which has nothing to do with Android resources (despite the same name).