Reputation: 5947
I'm having trouble getting getIdentifier
to work with a class variable. It's strange, because this works:
public Drawable getStationIcon(Context context) {
int resId = context.getResources().getIdentifier("m1m2_16", "drawable", "com.mypackage.namehere");
Drawable drawable = context.getResources().getDrawable(resId);
return drawable;
}
But this doesn't:
public Drawable getStationIcon(Context context) {
int resId = context.getResources().getIdentifier(this.stationIcon, "drawable", "com.mypackage.namehere");
Drawable drawable = context.getResources().getDrawable(resId);
return drawable;
}
And nor does this:
public Drawable getStationIcon(Context context) {
String stationI = this.stationIcon;
int resId = context.getResources().getIdentifier(stationI, "drawable", "com.mypackage.namehere");
Drawable drawable = context.getResources().getDrawable(resId);
return drawable;
}
And this.stationIcon
definitely equals m1m2_16
. I've tried other alternatives, ie using ""+this.stationIcon
, but nothing works when the first parameter is a variable. Is there something I'm missing?
Upvotes: 8
Views: 5388
Reputation: 764
Strangely it will probably work:
getIdentifier("com.mypackage.namehere:drawable/" + this.stationIcon, null, null);
Credit: https://stackoverflow.com/users/790997/idroid
Resources.getIdentifier() has unexpected behavior when name is numeric
Upvotes: 2