0

I've already tried to use

String mystring = getResources().getString(R.string.mystring);

And I found the same theme in this article How can I convert the android resources int to a string. eg.: android.R.string.cancel?. But it doesn't work in my situation.

This is my code:

public class Film{


  private int image;
    private String name;
    private  String schedule;
    private  String description;

    public Film() {
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
       ...
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

public class FilmList {


 public static   ArrayList<Film> getFilms(){
        ArrayList<Film>films=new ArrayList<>();
        Film film=null;

        film =new Film();
        film.setName(getResources().getString(R.string.name1));
        film.setImage(R.drawable.img1);
        film.setDescription(getResources().getString(R.string.desc1));
        films.add(film);

        film =new Film();
        film.setName(getResources().getString(R.string.name2));
        film.setImage(R.drawable.img2);
        film.setDescription(getResources().getString(R.string.desc2));
        films.add(film);

        return films
         }
    }
1
  • You can create resource instance in application class, and access it anywhere, because application class is live till your app is running. Commented May 21, 2018 at 17:18

3 Answers 3

1

If you don't have access to a Context (e.g. via an Activity, Service, ContentProvider, or BroadcastReceiver), you cannot get a string resource.

If you have a class that is not one of the above and it needs to get a string resource, you must either have pass a Context to that class so it can retrieve the resource, or pass that class an already-resolved resource.

Sign up to request clarification or add additional context in comments.

Comments

0

Make an application class and add a hold its reference in a static varialble. then you can access context anywhere in the app

public class MyApp extends Application {

    private static AppLWP instance;

    @Override
    public void onCreate() {
         super.onCreate();
         instance = this;
    }
 }

Then you can access this instance for context anywhere in the app

public static ArrayList<Film> getFilms(){
    ArrayList<Film>films=new ArrayList<>();
    Film film=null;

    film =new Film();
    film.setName(MyApp.instance.getResources()
    .getString(R.string.name1));
    film.setImage(R.drawable.img1);        
    film.setDescription(MyApp.instance.getResources()
    .getString(R.string.desc1));

    films.add(film);

    film =new Film();        
    film.setName(MyApp.instance.getResources()
    .getString(R.string.name2));
    film.setImage(R.drawable.img2);
    film.setDescription(MyApp.instance.getResources()
    .getString(R.string.desc2));
    films.add(film);

    return films
     }
}

Comments

0

First, provide context to the class Film via the activity you are calling the object of Film.

If you call from Main Activity you will have to do something like this:

public class MainActivty extends AppCompatActivity {

private static MainActivty obj;
onCreate() {
     //usual functions
     obj = MainActivity.this;
   }
}

Once you have the context, simply try this:

String mystring = context.getString(R.string.mystring);

Or, if you floowed my MainActivty method:

String mystring = MainActivty.obj.getResources().getString(R.string.mystring);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.