Can't use parseInt for images - Android

690 views Asked by At

I have a problem with putting different images in a list. I created a custom list and everything worked fine, except the images issue. My issue is with these line -

Integer p = Integer.parseInt("R.drawable.absolut");
holder.icon.setImageResource(p);

In the log I see the following error-

03-03 08:16:07.121: ERROR/AndroidRuntime(25486): java.lang.NumberFormatException: unable to parse 'R.drawable.absolut' as integer

What could cause this?

Thanks!

1

There are 1 answers

3
Konstantin Burov On BEST ANSWER

Parse int intended for parsing strings like "0", "100" etc and has nothing to do with what you're trying to achieve. You have to use the drawable id directly:

holder.icon.setImageResource(R.drawable.icon);

If for whatever reason you can't use the constant, correct way to get the drawable id would be:

Context context = getContext(); //obtain a context
int drawableId = context.getResources().getIdentifier("icon", "drawable", context.getPackageName());

But that's not a good practice at all and idicates that you have problems with your app design.