Issue
Is there a better way to do this:
int drawable;
switch(mSignalStrength) {
case 0:
drawable = R.drawable.signal_0;
break;
case 1:
drawable = R.drawable.signal_1;
break;
case 2:
drawable = R.drawable.signal_2;
break;
case 3:
drawable = R.drawable.signal_3;
break;
case 4:
drawable = R.drawable.signal_4;
break;
case 5:
drawable = R.drawable.signal_5;
break;
default:
drawable = -1;
}
I'm trying to replace the switch statement with some int getDrawableIdByString("signal_" + mSignalStrength)
function.
Solution
As someone mentioned in the comments, you can use the
getIdentifier(String name, String defType, String defPackage)
For you case, like this
int resId = getResources().getIdentifier("signal_" + mSignalStrength, "drawable", getPackageName());
Answered By - Marko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.