-3

I have created my own custom adapter. In this code I get getDrawable(int) is deprecated warning in this line: Drawable res = getContext().getResources().getDrawable(item.img_resId);.

I go through these:

Android getResources().getDrawable() deprecated API 22

Android: Alternative for context.getDrawable()

But none of them has work in my case.

My code:

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;
    }
}
Aashish
  • 2,355
  • 2
  • 22
  • 35
  • 1
    How exactly did the answers in those linked posts not work? Also, why are you retrieving the `Drawable` yourself? That is, why not just use `ImageView#setImageResource()`? – Mike M. Oct 10 '17 at 10:15
  • @MikeM. first link given in question will work. btw link is [https://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22/29041466](https://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22/29041466) – Aashish Oct 10 '17 at 10:18
  • Do you have access to `context` in the `Adapter`? Also, you may use `ImageView::setImageResource` for your purposes here. – Pavan Patil Oct 10 '17 at 10:19
  • I don't understand. If the answers in the first link work, why are you asking this question? – Mike M. Oct 10 '17 at 10:19
  • @MikeM. I think you are unable to open the first link so I write that it will work. Actually, answers have not fit for my problem. Btw now I got the solution for my problem. – Aashish Oct 10 '17 at 11:06

1 Answers1

2

Now you should use

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

Where this is your passed context if you are using a widget, just this if you are calling from an Activity, or getActivity() if you are calling from Fragment

In your case, when you are initializing your Widget also save your Context instance:

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

Now you are able to call:

Drawable drawable = ContextCompat.getDrawable(context,R.drawable.my_drawable);
Ricardo
  • 7,598
  • 3
  • 26
  • 33
  • this should not work. It shows wrong 1st argument type as an error. – Aashish Oct 10 '17 at 10:19
  • @NileshRathod not at all. This solution also given in 1st link in my question but not work in my case. – Aashish Oct 10 '17 at 10:28
  • @Ricardo now it will work. But still an error in the answer that in contructor CustomAdapter `super()` must be in the first line. – Aashish Oct 10 '17 at 10:42