I have added some manually icons to drawable
which for some manually data it have to take the drawable
and if a icon is not in drawable
then take it from the Glide
.
I tried something like till now but only it is getting the icons from drawable
.
I want to check if icon exists at drawable
take from there and if for an url the icon is not in drawable then go at another statement and take it from Glide
.
Below is my code.
The resID is always 0
String imageUrl = BASE_URL + arrayList.get(position).getSearchUrl() + "&size=32";
int resID = context.getResources().getIdentifier("icon", "drawable",context.getPackageName());
if (resID == 0) {
Log.d("TAG", "onBindViewHolder: " + resID);
viewHolder.tvIcon.setImageResource(arrayList.get(position).getIcon());
} else {
Log.d("TAG", "onBindViewHolder: Glide" + resID);
Glide.with(context)
.load(imageUrl)
.apply(requestOptions
.placeholder(R.drawable.default_favicon)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter())
.into(viewHolder.tvIcon);
}
You need to pass your arrayList.get(position).getIcon()
instead of "icon"
in context.getResources().getIdentifier()
Also read how getIdentifier()
works
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry".
Returns
: int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
Try this way
int resID = context.getResources().getIdentifier(String.valueOf(arrayList.get(position).getIcon()), "drawable",context.getPackageName()); "drawable",context.getPackageName());
// if resID == 0 means the icon is not available in drawable folder
// so it will load icon from url using Glide
if (resID == 0) {
Log.d("TAG", "onBindViewHolder: Glide" + resID);
Glide.with(context)
.load(imageUrl)
.apply(requestOptions
.placeholder(R.drawable.default_favicon)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter())
.into(viewHolder.tvIcon);
}
// if resID != 0 means the icon is available in drawable folder
// so it will load icon from drawable folder
else {
Log.d("TAG", "onBindViewHolder: " + resID);
viewHolder.tvIcon.setImageResource(resID);
}
int checkExistence = mContext.getResources().getIdentifier("my_resource_name", "drawable", mContext.getPackageName());
if ( `checkExistence != 0` ) { // the resouce exists...
result = true;
}
else { // checkExistence == 0 // the resouce does NOT exist!!
result = false;}
you are usingresID == 0
use not equal to zero.checkExistence != 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With