android - Get resource identifier from name -
i developing application reads resource name of each logo database , tries set drawables.
however, numberformatexception in logcat when try obtain integer identifier of logo , application force closes in beginning of application.
my code follows:
string logo; logo = c.getstring(2); button.setbackgroundresource(integer.parseint(logo)); logo saved in database example: r.drawable.logo
do have suggestion goes wrong here?
if value of logo "r.drawable.logo" (a string), can't parsed int. r.drawable.logo reference static int logo variable in static class drawable, subclass of generated resources class r. r generated resources class, can find in project under gen folder.
you have parse yourself. if know it's drawable returned, have like:
string logoparts [] = logo.split ("\\."); int logoid = getresources ().getidentifier (logoparts [logoparts.length - 1], "drawable", "com.example.app"); alternatively, can separate function:
public static int parseresourcestring (stinrg resstring, string package) { string resparts [] = resstring.split ("\\."); return getresources ().getidentifier (resparts [resparts.length - 1], resparts [resparts.length - 2], package); }
Comments
Post a Comment