How to get context in recycler view adapter

I am trying to use picasso library to load url in imageView, but I cannot get context to use picasso library correctly.

 public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> { private List<Post> mDataset; // Provide a reference to the views for each data item // Complex data items may need more than one view per item, and // you provide access to all the views for a data item in a view holder public class ViewHolder extends RecyclerView.ViewHolder { // each data item is just a string in this case public TextView txtHeader; public ImageView pub_image; public ViewHolder(View v) { super(v); txtHeader = (TextView) v.findViewById(R.id.firstline); pub_image = (ImageView) v.findViewById(R.id.imageView); } } // Provide a suitable constructor (depends on the kind of dataset) public FeedAdapter(List<Post> myDataset) { mDataset = myDataset; } // Create new views (invoked by the layout manager) @Override public FeedAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.feedholder, parent, false); // set the view size, margins, paddings and layout parameters ViewHolder vh = new ViewHolder(v); return vh; } // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder holder, int position) { // - get element from your dataset at this position // - replace the contents of the view with that element holder.txtHeader.setText(mDataset.get(position).getPost_text()); Picasso.with(this.context).load("http://i.imgur.com/DvpvklR.png").into(holder.pub_image); } // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return mDataset.size(); } } 
+95
java android android-context picasso android-recyclerview
Aug. 21 '15 at 9:29
source share
10 answers

You have several options:

  • Pass Context as an argument to FeedAdapter and save it as a class field
  • Use dependency injection to enter Context when you need it. I highly recommend reading about it. There's a great tool for that - Dagger Square
  • Get it from any View object. In your case, this might work for you:

    holder.pub_image.getContext()

    Like pub_image there is an ImageView .

+192
Aug 21 '15 at 9:35
source share

You can add a global variable:

 private Context context; 

then assign the context here:

 @Override public FeedAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) { // create a new view View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.feedholder, parent, false); // set the view size, margins, paddings and layout parameters ViewHolder vh = new ViewHolder(v); // set the Context here context = parent.getContext(); return vh; } 

Happy Codding :)

+21
Jan 19 '16 at 9:44
source share

You can use the pub_image context ( holder.pub_image.getContext() ):

 @Override public void onBindViewHolder(ViewHolder ViewHolder, int position) { holder.txtHeader.setText(mDataset.get(position).getPost_text()); Picasso.with(holder.pub_image.getContext()).load("http://i.imgur.com/DvpvklR.png").into(holder.pub_image); } 
+12
Apr 16 '16 at 14:41
source share

Short answer:

 Context context; @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); context = recyclerView.getContext(); } 

Explanation why the other answers are not great:

  1. Passing Context to the adapter is completely unnecessary since RecyclerView you can access it from inside the class
  2. Getting Context at the ViewHolder level means that you do this every time you link or create a ViewHolder . You duplicate operations.
  3. I do not think you need to worry about memory leak. If your adapter is delayed beyond the Activity period of your Activity (which would be strange), then you already have a leak.
+6
Feb 07 '18 at 10:00
source share

Create the FeedAdapter constructor:

 Context context; //global public FeedAdapter(Context context) { this.context = context; } 

and in action

 FeedAdapter obj = new FeedAdapter(this); 
+4
Aug 21 '15 at 9:31 on
source share

Declare globally first

Context mContext;

pass the context using the constructor by changing it.

 public FeedAdapter(List<Post> myDataset, Context context) { mDataset = myDataset; this.mContext = context; } 

then use mContext if you need it

+4
Aug 21 '15 at 9:33
source share

You can use this view.getContext ()

Example

 holder.tv_room_name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(), "", Toast.LENGTH_SHORT).show(); } }); 
+4
Jul 13 '16 at 11:03
source share

First add a global variable

 Context mContext; 

Then change your constructor to

 public FeedAdapter(Context context, List<Post> myDataset) { mContext = context; mDataset = myDataset; } 

Skip your context when creating the adapter.

 FeedAdapter myAdapter = new FeedAdapter(this,myDataset); 
+2
Aug 21 '15 at 9:40
source share

you can use this:

 itemView.getContext() 
+2
May 04 '17 at 16:43
source share

You can define:

 Context ctx; 

And on onCreate initialize ctx :

 ctx=parent.getContext(); 

Note. Parent is a ViewGroup.

0
Dec 20 '18 at 21:15
source share



All Articles