1

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.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
Txispas
  • 159
  • 4
  • 14
  • 2
    Pass ``Util`` constructor a reference to your Activity, or ``Context`` to be more precise. From ``Context`` you can access ``getString`` method and localised strings. – harism Aug 27 '12 at 12:52

2 Answers2

0

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;
} 
user987760
  • 1,061
  • 3
  • 12
  • 26
0

have you thought about using a string array? It would be so much simpler than what you are trying to do.

I do not think that your concerns about language change are justified, in terms of the overall user experience.

Philip Sheard
  • 5,789
  • 5
  • 27
  • 42