Search code examples
androidgridviewonitemclicklistenergetstring

getstring context with onitemclicklistener


I have the following class define with some info that I would like to catch from another one. I used the context so getstring() can work:

public class Util{

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;

}

Then in another class extend activity I call to this class on the OnListItemClick() and works perfect. But in another class which charge a gridview instead of a list I use OnItemClicklistener() and doesn´t work, thats the code:

gridView.setOnItemClickListener(new OnItemClickListener() {        
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        Util u = new Util();
        ArrayList<RecetaBean> Recetas = u.getRellenas();

The error is:

The method getRellenas(Context) in the type Util is not applicable for the arguments (new AdapterView.OnItemClickListener(){})

I don´t know how to implement that and fix it! Thanks!


Solution

  • In your

    Utils.getRellenas(Context con)
    

    you expect a Context as parameter. I think it might be solved like this

    ArrayList<RecetaBean> Recetas = u.getRellenas(MyActivityName.this);
    

    Where you replace "MyActivityName" with the name of your containing activity.