이 가능하다고는, 다음을 수행해야 Drawable ", 세 번째 패키지 이름은 getPackageName()
메서드를 호출하여 컨텍스트에서 얻은 패키지 이름입니다. ID가 문자열이 아니기 때문에 내가 '처럼이 해결 방법을 가지고 있지만 (당신이 생각했다으로 이해되지 않는다 문자열을 정수로 캐스팅
View myView = findViewById(myId);
: 그런 다음 나중에 다음 코드보기를 얻을 수 있습니다 너를 보여주는 m).
업데이트
당신이 Activity
클래스의 외부에서이 코드를 사용하고 있기 때문에 Context
방법에 대한 호출이 유효하지 않습니다. 활동 컨텍스트에 액세스하는 방법을 만들 필요가 있습니다 (클래스에서 .getContext()
를 호출하는 것을 언급하지만 활동이 아니라 해당 클래스 컨텍스트를 가져옵니다). 당신의 생성자를 수정하는 것이 달성의 방법은, 당신이 말할 수 myClass
라는 클래스 :
class myClass{
//Declase a Context variable inside your class
Context x;
//You implement a constructor for this class that accepts a Context as
//a parameter (feel free to add more if you are using a constructor already)
public myClass(Context applicationContext){
//Assign the passed value to your local Context
x = applicationContext;
}
//Afterwards, on a different part of your class, you could invoke activity
//related methods by using the Context you have 'x'
public void otherMethod(){
int myId = x.getResources().getIdentifier("R.drawable.scoredisplay" + i, "drawable",x.getPackageName());
}
}
,이 곳과 비슷한 무언가를 볼 수 당신이 당신의 Activity
에서 제대로 값을 전달하는 보장의 마지막 부분 코드에서 :
: 우리가 지금 생성자를 가지고, 또는 기존의 것을 수정 한 이후
myClass i = new myClass();
, 당신은 바로 게임이나 어떤 클래스로 만들기 활동 컨텍스트를 전달하는 this
을 추가 할 수 있습니다
myClass i = new myClass(this);//'this' can be 'getApplicationContext()'