Issue
My application is a basic notepad style application where the main activity is a list and each item can be edited. The data for each item includes the name of the item and then a list of text fields. I currently have a class to manage the items as follows:
public class Item implements Serializeable {
private String name;
private String review;
public Item() {
name = Resources.getSystem().getString(R.string.name_movie);
description = Resources.getSystem().getString(R.string.description_like);
}
public Item(String itemname, String itemreview) {
//... followed by getters and setters
}
then in <strings.xml>
<string name="name_movie">movie</string>
<string name="name_movie">book</string>
<string name="name_movie">record</string>
<string name="description_like">good</string>
<string name="description_love">great</string>
etc. The are all translated into other languages and I can refer to them in code as
R.string.name_movie
R.string.description_like
etc. I'm summarizing because you get the idea.
The problem is the default constructor where I'd like to assign values based on string resources. Unfortunately, to get system resources requires a context. These are POJO classes and have no context. Additionally, I the main activity holds an arraylist of items into a file so I've made the items serializeable. Because there are serializeable I need a no argument constructor so I can't use a static member variable for the context with a constructor such as follows:
public Item(Context context) {
m_context = context;
}
My question is in an android application what is the best way to access multilingual strings in a plain old java object that is serializeable? I'm reviewed the following discussions, among others: getString Outside of a Context or Activity, Access string.xml Resource File from Java Android Code, and Static way to get 'Context' in Android?.
What other ways are there that I could use localized strings in these objects? I considered storing the resource identifier instead of the string in my item but I want to leave it open to modify my application to take user entered strings rather than just picking from a list.
Solution
Using Application class. Extend Android Application class, add a static variable Context and onCreate Method assign application context to it. Add a method get contact Wich return your application context. And inside your pojo, you can have access it by Application.MyMethod() and so. ( Don't forget to assign your application class to your manifest.
Answered By - user4752077
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.