I\'m trying to use picasso library to be able to load url to imageView, but I\'m not able to get the context
to use the picasso library correctly.
You can add global variable:
private Context context;
then assign the context from 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's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
// set the Context here
context = parent.getContext();
return vh;
}
Happy Codding :)
You can define:
Context ctx;
And on onCreate
initialise ctx
to:
ctx=parent.getContext();
Note: Parent is a ViewGroup.
First add a global variable
Context mContext;
Then change your constructor to this
public FeedAdapter(Context context, List<Post> myDataset) {
mContext = context;
mDataset = myDataset;
}
The pass your context when creating the adapter.
FeedAdapter myAdapter = new FeedAdapter(this,myDataset);
First globally declare
Context mContext;
pass context with the constructor, by modifying it.
public FeedAdapter(List<Post> myDataset, Context context) {
mDataset = myDataset;
this.mContext = context;
}
then use the mContext
whereever you need it
You have a few options here:
Context
as an argument to FeedAdapter and keep it as class fieldContext
when you need it. I strongly suggest reading about it. There is a great tool for that -- Dagger by SquareGet it from any View
object. In your case this might work for you:
holder.pub_image.getContext()
As pub_image
is a ImageView
.
You can use 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);
}