此行中不推荐使用getDrawable(int)警告:Drawable res = getContext()。getResources()。getDrawable(item.img_resId);

时间:2017-10-10 10:12:30

标签: java android deprecated

我已经创建了自己的自定义适配器。在这段代码中,我在这行中得到了getDrawable(int)不推荐的警告:T3ID

我经历过这些:

Android getResources().getDrawable() deprecated API 22

Android: Alternative for context.getDrawable()

但是我的情况都没有。

我的代码:

Drawable res = getContext().getResources().getDrawable(item.img_resId);

1 个答案:

答案 0 :(得分:1)

现在你应该使用

Drawable drawable = ContextCompat.getDrawable(this,R.drawable.my_drawable);

如果你使用的是小部件this,那么context就是this,如果你是来自某个活动,则只需getActivity(),如果你是从Fragment打电话,则Context

在您的情况下,当您初始化您的Widget时,还要保存您的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);

现在您可以致电:

const dataPromise = fetch('http://api.tvmaze.com/schedule?country=GB');

let newArray;

dataPromise
  .then(data => data.json())
  .then(data => {
    //console.log(data) // This works
    newArray = data.map((item)=>{
      return item.name;
    });
  })
  .catch((err)=> {
    console.error(err);
  });

  console.log(newArray); // This doenst work