How to get a context in a recycler view adapter
You have a few options here:
- Pass
Context
as an argument to FeedAdapter and keep it as class field - 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 Get it from any
View
object. In your case this might work for you:holder.pub_image.getContext()
As
pub_image
is aImageView
.
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
Change adapter to take
Activity
notContext
. like belowpublic class VideosAdapter extends RecyclerView.Adapter<VideosAdapter.VideosViewHolder> {
private Activity activity;
..
public VideosAdapter(Activity activity, Video[] data, int layout) {
this.activity = activity;
...
}The second way to fix this is, change
getAdRequest(Activity activity)
, like belowpublic 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
Create a New Textview Programmatically Then Display It Below Another Textview
Ubuntu: Openjdk 8 - Unable to Locate Package
Aes Java Encoding, Ruby Decoding
Converting a Date String to a Datetime Object Using Joda Time Library
Cannot Issue Data Manipulation Statements with Executequery()
Who Sets Response Content-Type in Spring MVC (@Responsebody)
The Literal Xyz of Type Int Is Out of Range
What Is the Use of Hashcode in Java
What Is the Default Access Specifier in Java
What Does "Error: '.Class' Expected" Mean and How to Fix It
Trying to Locate a Leak! What Does Anon Mean for Pmap
Interfaces with Static Fields in Java for Sharing 'Constants'
What Is the Maximum Depth of the Java Call Stack
How to Make Java Print Quotes, Like "Hello"
Where Does Java's String Constant Pool Live, the Heap or the Stack