I have the following code for getting a resource using name instead of id:
spec = new ImageView[56];
for (i=2; i<=56; i++) {
String s = null;
s = "items_r"+Integer.toString(i)+"_c1";
spec[i] = (ImageView) findViewById(getResources().getIdentifier(s,"drawable",getPackageName()));
Log.e(tag, Boolean.toString(spec[i] == null));
}
I have 56 items in my drawable folder named items_r1_c1 to items_r56_c1;
But I am getting spec[i] as null.
Could someone point me towards my error and how to correct it?
Thank you.
findViewById(int)is for findingViews that have already been inflated and that currently exist under your Activity’s content view.You’ll want to use
Resources.getDrawable(int)orImageView.setImageResource(int).