O
O
Oleksiy2016-10-20 17:03:43
Java
Oleksiy, 2016-10-20 17:03:43

How to take filename from db and match it to file in drawable?

Good day everyone!
There is a DB in which (in a separate column) names of files (for example, pic1.jpg etc.) are stored.
In this case, the file itself is located in the drawable folder.
At the moment I have the following in the adapter:
ImageView ob_pic = (ImageView) my_view.findViewById(R.id.ob_pic);
ob_pic.setImageResource(coin.getObPic());
Obviously it doesn't work. It's not clear - how to write the mapping in the adapter correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mitaichik, 2016-10-20
@xox_ua

https://developer.android.com/reference/android/co... , java.lang.String, java.lang.String)
But just in case, I'll ask: Are you sure this is the right approach?

A
AlexeyVD, 2016-10-20
@AlexeyVD

public int getDrawableRes(String resName) {
        try {
            Field idField = R.drawable.class.getDeclaredField(resName);
            return idField.getInt(null);
        } catch (Exception e) {
            // ...
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question