Java getDrawable(int)是此行中不推荐使用的警告:Drawable res=getContext().getResources().getDrawable(item.img_resd);

Java getDrawable(int)是此行中不推荐使用的警告:Drawable res=getContext().getResources().getDrawable(item.img_resd);,java,android,deprecated,Java,Android,Deprecated,我已经创建了自己的自定义适配器。在这段代码中,我得到的getDrawableint是不推荐使用的警告:DrawableRes=getContext.getResources.getDrawableitem.img_resId 我经历了这些: 但在我的情况下,他们都没有工作 我的代码: 现在你应该使用 Drawable drawable = ContextCompat.getDrawable(this,R.drawable.my_drawable); 其中,如果您使用的是小部件,则这是传递的上下

我已经创建了自己的自定义适配器。在这段代码中,我得到的getDrawableint是不推荐使用的警告:DrawableRes=getContext.getResources.getDrawableitem.img_resId

我经历了这些:

但在我的情况下,他们都没有工作

我的代码:

现在你应该使用

Drawable drawable = ContextCompat.getDrawable(this,R.drawable.my_drawable);
其中,如果您使用的是小部件,则这是传递的上下文;如果您是从活动调用,则这是传递的上下文;如果您是从片段调用,则这是传递的上下文

在您的情况下,在初始化小部件时,还可以保存上下文实例:

Context context;
public CustomAdapter(Context context, ArrayList<ItemDataObject> list_item_details) {
    super(context, 0, list_item_details);
    this.context = context;
}

这些链接帖子中的答案究竟是如何不起作用的?还有,你为什么要自己取回可提取的文件?也就是说,为什么不直接使用ImageViewsetImageResource?@MikeM。给出的第一个链接将起作用。顺便说一句,链接是您是否有权访问适配器中的上下文?另外,你可以在这里使用ImageView::setImageResource,我不明白。如果第一个链接中的答案有效,你为什么要问这个问题?@MikeM。我想你无法打开第一个链接,所以我写它将工作。事实上,答案并不适合我的问题。顺便说一句,现在我找到了解决问题的办法。这不应该奏效。它将错误的第一个参数类型显示为错误。@NileshRathod一点也不。这个解决方案在我的问题的第一个链接中也给出了,但在我的情况下不起作用。@Ricardo现在它会起作用。但答案中仍然存在一个错误,即在constructor CustomAdapter super中必须位于第一行。
Context context;
public CustomAdapter(Context context, ArrayList<ItemDataObject> list_item_details) {
    super(context, 0, list_item_details);
    this.context = context;
}
Drawable drawable = ContextCompat.getDrawable(context,R.drawable.my_drawable);