How to Get a Context in a Recycler View Adapter

How to get a context in a recycler view adapter

You have a few options here:

  1. Pass Context as an argument to FeedAdapter and keep it as class field
  2. Use dependency injection to inject Context when you need it. I strongly suggest reading about it. There is a great tool for that -- Dagger by Square
  3. Get it from any View object. In your case this might work for you:

    holder.pub_image.getContext()

    As pub_image is a ImageView.

Get context in onBindViewHolder in RecyclerView Adapter

You don't have to take context as a constructor param. every view has reference to context

@Override
public void onBindViewHolder(@NonNull Holder holder, int i) {
Context context = holder.itemview.getContext();
}

Context on Adapter for RecyclerView

set an id to root element of adapter item layout, then find that view by id and use it instead of itemView ,
instead of :

subsidioViewHolder.itemView.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
...
}
});

use:

subsidioViewHolder.rootLayout.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
...
}
});

. and remember you can get context in recyclerView adapter inside onCreateViewHolder() without using constructor like this:

@Override
public SubsidioViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
context=viewGroup.getContext();
View view = this.inflater.inflate(R.layout.producto_item, null);
return new SubsidioViewHolder(view);
}

although it seems like that your problem is with

Get context within RecyclerView.ViewHolder

you can use itemView.context() to get the context

just context() is enough

How do I extablish context for an Activity within a recyclerview adapter

In the constructor of CheckInListAdapter pass another argument as context

public CheckInListAdapter(int layoutId, Context context) {
Log.i(TAG, "CheckInListAdapter: ");
checkInListLayout = layoutId;
this.context = context
}

getActivity() in adapter

It's just because you are sending Context object to the method getAdRequest(Activity activity) which needs Activity object. So you can fix it in 2 ways

  1. Change adapter to take Activity not Context. like below

    public class VideosAdapter extends RecyclerView.Adapter<VideosAdapter.VideosViewHolder> {
    private Activity activity;
    ..

    public VideosAdapter(Activity activity, Video[] data, int layout) {
    this.activity = activity;
    ...
    }
  2. The second way to fix this is, change getAdRequest(Activity activity), like below

    public static AdRequest getAdRequest(Context context) {
    return new AdRequest.Builder()
    .addNetworkExtrasBundle(AdMobAdapter.class, GDPR.getBundleAd(context))
    .build();
    }

But in the second way, I am not sure if GDPR.getBundleAd() can take context. So better to go with 1st way


3rd way is to typecast context as Activity as you commented. Like

mInterstitialAd.loadAd(GDPR.getAdRequest((Activity) context));

But there is one problem in this approach. As the constructor of your adapter take Context parameter, any Activity which using this adapter can create the object like new VideosAdapter(activity, ...) BUT can create the object like new VideosAdapter(applicationContext, ...) or new VideosAdapter(objectOfcontext, ...). So in the last 2 cases, you will get a typecast exception.



Related Topics



Leave a reply



Submit