我有一个简单类的问题,其中包含我想从另一个类调用的信息。 例如,这里是包含信息的类Util :
public class Util{ public ArrayList<RecetaBean> getRellenas() { ArrayList<RecetaBean> MiLista = new ArrayList<RecetaBean>(); RecetaBean receta1 = new RecetaBean(); String ingrediente1[] = { getString(R.string.app_name),getString(R.string.app_name), }; receta1.setIngredientesLista(ingrediente1); MiLista.add(receta1); MiLista.add(receta1); MiLista.add(receta1); return MiLista; } }然后在另一个类中我得到像这样调用的Items:
Util u = new Util(); ArrayList<RecetaBean> Recetas = u.getRellenas();所以,我在使用GETSTRING的类Util中GETSTRING了执行问题,因为我想得到一个不同的字符串(因为语言不同)。 退出错误的方法是从Activity扩展类Util ,但Util不是Activity ! 如果我从Activity扩展,应用程序崩溃。
I have a problem with a simple class that contains information that I will like to call from another class. For example here is the class Util which contains the info:
public class Util{ public ArrayList<RecetaBean> getRellenas() { ArrayList<RecetaBean> MiLista = new ArrayList<RecetaBean>(); RecetaBean receta1 = new RecetaBean(); String ingrediente1[] = { getString(R.string.app_name),getString(R.string.app_name), }; receta1.setIngredientesLista(ingrediente1); MiLista.add(receta1); MiLista.add(receta1); MiLista.add(receta1); return MiLista; } }Then in another class I get the Items calling like this:
Util u = new Util(); ArrayList<RecetaBean> Recetas = u.getRellenas();So, I have a execution problem in the class Util with the GETSTRING, because I would like to get a different string (because of different languages). The way to quit the error is to extend the class Util from Activity, but Util is not an Activity! And if I extend from Activity, the app crash.
最满意答案
您需要做的就是在方法中定义一个上下文。
并称之为这样;
Util u = new Util(); ArrayList<RecetaBean> Recetas = u.getRellenas(this);和你在你的上下文这样的方法:
public ArrayList<RecetaBean> getRellenas(Context con) { ArrayList<RecetaBean> MiLista = new ArrayList<RecetaBean>(); RecetaBean receta1 = new RecetaBean(); String ingrediente1[] = { con.getString(R.string.app_name), con.getString(R.string.app_name), }; receta1.setIngredientesLista(ingrediente1); MiLista.add(receta1); MiLista.add(receta1); MiLista.add(receta1); return MiLista; }all you need to do is Define a Context in the Method .
and call it like this;
Util u = new Util(); ArrayList<RecetaBean> Recetas = u.getRellenas(this);and you the context in you Methods like this:
public ArrayList<RecetaBean> getRellenas(Context con) { ArrayList<RecetaBean> MiLista = new ArrayList<RecetaBean>(); RecetaBean receta1 = new RecetaBean(); String ingrediente1[] = { con.getString(R.string.app_name), con.getString(R.string.app_name), }; receta1.setIngredientesLista(ingrediente1); MiLista.add(receta1); MiLista.add(receta1); MiLista.add(receta1); return MiLista; }更多推荐
发布评论