Get resource identifier on behalf of

I am developing an application that reads the resource name of each logo from a database and then tries to install the drawings.

However, I get NumberFormatExceptionin my Logcat when I try to get an integer logo identifier, and the application suddenly forcibly closes at the beginning of the application.

My code is as follows:

String logo;
logo = c.getString(2);
button.setBackgroundResource(Integer.parseInt(logo));

logo stored in the database, for example: R.drawable.logo

Do you have any suggestion on what's wrong here?

+5
source share
2 answers

logo "R.drawable.logo" (a String), int. R.drawable.logo static int logo static class drawable, R. R - , gen.

. , , - :

String logoParts [] = logo.split ("\\.");
int logoId = getResources ().getIdentifier (logoParts [logoParts.length - 1], "drawable", "com.example.app");

:

public static int parseResourceString (Stinrg resString, String package) {
    String resParts [] = resString.split ("\\.");
    return getResources ().getIdentifier (resParts [resParts.length - 1], resParts [resParts.length - 2], package);
}
+2

String logo=c.getString(2);

, R.drawable, , .

logo=logo.split("\\.")[2];

-

int drawableId = getResources().getIdentifier(logo, "drawable", "com.mypackage.myapp");

button.setBackgroundResource(drawableId);
0

All Articles