2012-08-27 3 views
1

다른 클래스에서 호출하려는 정보가 들어있는 간단한 클래스에 문제가 있습니다.확장 활동이없는 호출에서 getstring()

Util u = new Util(); 
    ArrayList<RecetaBean> Recetas = u.getRellenas(); 

그래서, 나는 클래스 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; 
} 
    } 

그런 다음 다른 클래스에서 나는 항목은 다음과 같이 호출 얻을 예를 들어 여기에 정보를 포함하는 클래스 Util입니다 GETSTRING, 왜냐하면 나는 다른 문자열 때문에 (다른 언어 때문에) 얻고 싶기 때문이다. 오류를 종료하는 방법은 UtilActivity에서 연장하지만 UtilActivity이 아닙니다! 그리고 Activity에서 확장하면 앱이 다운됩니다.

+2

''Util'' 생성자 나 Activity에 대한 참조를 전달하거나,보다 정확하게하기 위해''Context''를 전달하십시오. ''Context''에서''getString'' 메서드와 지역화 된 문자열에 접근 할 수 있습니다. – harism

답변

0

메서드에서 컨텍스트 정의 만하면됩니다.

이렇게 말하면됩니다.

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; 
} 
+0

그 덕분에 완벽하게 작동합니다! – Txispas

0

당신이 문자열 배열을 사용하는 방법에 대한 생각? 그것은 당신이하려고하는 것보다 훨씬 더 간단합니다.

나는 언어 변경에 대한 귀하의 우려가 전반적인 사용자 경험의 측면에서 정당하다고 생각하지 않습니다.

관련 문제