Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

In android I am looping through the database and assigning text and image:

Cursor res = myDb.getAllData();
while (res.moveToNext()) {
    Actors actor = new Actors();
    actor.setName(res.getString(1));
    String th =  res.getString(11);
    Integer thumb = this.getResources().getIdentifier(th, "drawable", "mypackage");
    actor.setThumb(R.drawable.th);

However Lint suggests not to use getIdentifier - Use of this function is discouraged because resource reflection makes it harder to perform build optimizations and compile-time verification of code.

In database column I have just the image name (string). How can I replace getIdentifier?

Even if I change the DB column maybe directly to R.drawable.imagename, it is still a string and for setThumb I need a drawable.

How many images are there? You could use a switch statement, or build and cache a Map<String, Integer> of string-to-identifer values and use that. – CommonsWare Jan 24 at 21:45 there are around 200 images. They are all in drawable folder, I just want to list them according to their name in database. I know I can use also Picasso library, but rather no additional libraries. For now, getIdentifier works well, but as I mentioned, it is not suggested to use. – Darksymphony Jan 25 at 18:27 I believe It's time to do database migration from strings to integers as a first step; then everything will be straightforward – Zain Jan 29 at 14:28 and what will be in the database columns as integer? I need the image name there. Even if you have an integer in database, you can't use it dynamically in code as drawable, because drawable is drawable, unless you store the drawable ID in database, which I don't know for all 200 images and will be more in the future – Darksymphony Jan 30 at 13:18 Just saw you did find a solution through reflection, viola!. No need to put an answer I guess; if you still need it please drop me a tag. – Zain Feb 1 at 11:18

Ok, so the only solution what I've found is here https://stackoverflow.com/a/4428288/1345089

public static int getResId(String resName, Class<?> c) {
        try {
            Field idField = c.getDeclaredField(resName);
            return idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;

and then just calling:

int resID = getResId("icon", R.drawable.class);

This works very well, however some users reports, that after installing the app from Play store (not my app, but any with this method implemented and proguard enabled), after a while it will start throwing NoSuchFieldException, as more resources are mapped to the same class/field.

It can be caused by proguard but not sure. The solution is then to put the old way getResources().getIdentifier to the exception part of code.

well, I did not find another solution, and some blogs posted this is about 5 times faster than with getIdentifier. I don't know about any other solution – Darksymphony Feb 1 at 17:01 could you please share the blog post link where they state this approach is 5 times faster? thanks – Lino Feb 3 at 9:50

then use the methode below:

for (int i = 0; i < RessourceQtt; i++) {
        Uri path1 = Uri.parse("android.resource://yourPackage Directories/drawable/ressourceName0" + i);
        list.add(new CarouselItem(String.valueOf(path1)));

You can try this my code three way

// Show message and quit
    Application app = cordova.getActivity().getApplication();
    String package_name = app.getPackageName();
    Resources resources = app.getResources();
    String message = resources.getString(resources.getIdentifier("message", "string", package_name));
    String label = resources.getString(resources.getIdentifier("label", "string", package_name));
    this.alert(message, label);

set icon like that

private int getIconResId() {
Context context = getApplicationContext();
Resources res = context.getResources();
String pkgName = context.getPackageName();
int resId;
resId = res.getIdentifier("icon", "drawable", pkgName);
return resId;

this is also

//set icon image
final String prefix = "ic_";
String icon_id = prefix + cursor.getString(cursor.getColumnIndex(WeatherEntry.COLUMN_ICON));
Resources res = mContext.getResources();
int resourceId = res.getIdentifier(icon_id, "drawable", mContext.getPackageName());
viewHolder.imgIcon.setImageResource(resourceId);

I hope this code help for you.

public static int getIcId(String resN, Class<?> c) {
try {
    Field idF = c.getDeclaredField(resN);
    return idF.getInt(idF);
} catch (Exception e) {
    throw new RuntimeException("No resource ID found for: "
            + resN+ " / " + c, e);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.