来自没有扩展活动的调用的getstring()(getstring() from a call without extend activity)

编程入门 行业动态 更新时间:2024-10-25 04:23:41
来自没有扩展活动的调用的getstring()(getstring() from a call without extend activity)

我有一个简单类的问题,其中包含我想从另一个类调用的信息。 例如,这里是包含信息的类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; }

更多推荐

本文发布于:2023-07-26 02:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:call   getstring   activity   extend

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!