【问题标题】:getDrawable(int) is deprecated warning in this line: Drawable res = getContext().getResources().getDrawable(item.img_resId);getDrawable(int) 在这一行中被弃用警告: Drawable res = getContext().getResources().getDrawable(item.img_resId);
【发布时间】:2017-10-10 10:12:29
【问题描述】:

我创建了自己的自定义适配器。在这段代码中,我在这一行中得到 getDrawable(int) is deprecated 警告:Drawable res = getContext().getResources().getDrawable(item.img_resId);

我经历了这些:

Android getResources().getDrawable() deprecated API 22

Android: Alternative for context.getDrawable()

但在我的情况下,它们都不起作用。

我的代码:

public class CustomAdapter extends ArrayAdapter<ItemDataObject> {
private static class ViewHolder {
    ImageView img;
    TextView title;
    TextView sub_title;
}

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

@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    // Get the data item for this position
    ItemDataObject item = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag
    if (convertView == null) {
        // If there's no view to re-use, inflate a brand new view for row
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.listview_item, parent, false);
        viewHolder.img = convertView.findViewById(R.id.img);
        viewHolder.title = convertView.findViewById(R.id.tvTitle);
        viewHolder.sub_title = convertView.findViewById(R.id.tvSubTitle);
        // Cache the viewHolder object inside the fresh view
        convertView.setTag(viewHolder);
    } else {
        // View is being recycled, retrieve the viewHolder object from tag
        viewHolder = (ViewHolder) convertView.getTag();
    }
    // Populate the data from the data object via the viewHolder object into the template view.
    assert item != null;
    Drawable res = getContext().getResources().getDrawable(item.img_resId);
    viewHolder.img.setImageDrawable(res);
    viewHolder.title.setText(item.title);
    viewHolder.sub_title.setText(item.subtitle);
    // Return the completed view to render on screen
    return convertView;
    }
}

【问题讨论】:

  • 这些链接帖子中的答案究竟是如何不起作用的?另外,您为什么要自己检索Drawable?也就是说,为什么不直接使用ImageView#setImageResource()
  • @MikeM。有问题的第一个链接将起作用。顺便说一句,链接是https://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22/29041466
  • 您是否可以访问Adapter 中的context?此外,您可以在这里使用ImageView::setImageResource
  • 我不明白。如果第一个链接中的答案有效,您为什么要问这个问题?
  • @MikeM。我认为您无法打开第一个链接,所以我写它会起作用。实际上,答案不适合我的问题。顺便说一句,现在我找到了解决问题的方法。

标签: java android deprecated


【解决方案1】:

现在你应该使用

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

如果您使用小部件,this 是您传递的 context,如果您从 Activity 调用,则为 this,如果您从 Fragment 调用,则为 getActivity()

在您的情况下,当您初始化 Widget 时,还要保存您的 Context 实例:

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

【讨论】:

  • 这应该不起作用。它将错误的第一个参数类型显示为错误。
  • @NileshRathod 根本没有。这个解决方案也在我的问题的第一个链接中给出,但在我的情况下不起作用。
  • @Ricardo 现在它可以工作了。但是在contructor CustomAdapter super() 必须在第一行的答案中仍然存在错误。
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-28
  • 1970-01-01
  • 2023-03-02
  • 2012-06-06
相关资源
最近更新 更多