I try to use setImageResources()
with a way to set a R.drawable. variable_image_file . I have no idea how to do it, so I searched a little and i found this on many forums:
int resID = getResources().getIdentifier(character.getBmppath(), "drawable", getPackageName());
holder.pictureView.setImageResource(resID);
But here is my problem, Android Studio seems to not recognize getResources()
and getPackageName()
:
(in this code, 'domas.png' is stored in the drawable folder)
I don't find a solution online, can you please help to figure this out ?
Pass a Context
to your class and then use getResources
on it.
context.getResources().getIdentifier("domas", "drawable", context.getPackageName());
I would suggest you setting image this way
holder.pictureView.setImageDrawable(
ContextCompat.getDrawable(
context,
R.drawable.domas
)
)
如果您的适配器中没有上下文,请尝试此操作。
int resId =holder.firstNameView.getContext().getResources().getIdentifier("domas", "drawable", holder.firstNameView.getContext().getPackageName());
If it is in your drawable
folder, you don't need to do anything fancy, this should work after providing a Context
(I assume you can easily convert the below to Java from Kotlin):
context?.let {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
AppCompatResources.getDrawable(it, R.drawable.domas)
} else {
it.resources.getDrawable(R.drawable.domas, null)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.