gpt4 book ai didi

android - 从 url 获取图标,从 drawable 获取图标,它只从 drawable 获取

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:07:29 31 4
gpt4 key购买 nike

我在 drawable 中添加了一些手动图标,对于一些手动数据,它必须采用 drawable 并且如果图标不在 drawable 然后从 Glide 中获取它。

我试过类似的东西,但只有它从 drawable 获取图标。

我想检查图标是否存在于 drawable 从那里获取,如果对于一个 url,图标不在 drawable 则转到另一个语句并从 Glide 获取它.

下面是我的代码。
resID始终为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);
}

最佳答案

您需要传递您的 arrayList.get(position).getIcon() 而不是 "icon"context.getResources().getIdentifier()

另请阅读如何 getIdentifier()作品

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.)

试试这个

    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);
}

关于android - 从 url 获取图标,从 drawable 获取图标,它只从 drawable 获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54570579/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com