BENN1TH BENN1TH - 22 days ago 8
Android Question

Cant resolve context for picasso in android

I have a custom adapter that has data from json including a image url. The issue is I am getting "cant resolve symbol" on the context value inside the onBindViewHolder() ...

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {

ClubFieldsModel value = Items.get(i);
//Uri uri = Uri.parse(value.getFieldImageThumb().toString());
viewHolder.title.setText(value.getTitle());
viewHolder.desc.setText(value.getDesc());
String imageUrl = value.getFieldImageThumb();

//context "cant resolve symbol" error ...
Picasso.with(context).load(imageUrl).into(viewHolder.field_image_thumb);

}


Cant see why the issue would exist, everything is initialized..

EDIT: Full Adapter Code..

package app.sync.myapp.adapters;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.net.URI;
import java.util.List;


import app.sync.myapp.R;
import app.sync.myapp.model.ClubFieldsModel;

import static android.support.v7.appcompat.R.id.action_context_bar;
import static android.support.v7.appcompat.R.id.image;
import static app.sync.myapp.R.id.field_image_thumb;

public class ClubFieldsAdapter extends RecyclerView.Adapter<ClubFieldsAdapter.ViewHolder> {

List<ClubFieldsModel> Items;

public ClubFieldsAdapter(List<ClubFieldsModel> Items) {
this.Items = Items;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.club_fields_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {

ClubFieldsModel value = Items.get(i);
//Uri uri = Uri.parse(value.getFieldImageThumb().toString());
viewHolder.title.setText(value.getTitle());
viewHolder.desc.setText(value.getDesc());
String imageUrl = value.getFieldImageThumb();

Picasso.with(context).load(imageUrl).into(viewHolder.field_image_thumb);

}

@Override
public int getItemCount() {
return Items.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

public ImageView field_image_thumb;
public TextView title;
public TextView desc;

public ViewHolder(View itemView) {

super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
desc = (TextView) itemView.findViewById(R.id.desc);
field_image_thumb = (ImageView) itemView.findViewById(R.id.field_image_thumb);


}


}
}

Answer

you can use viewHolder.itemView.getContext(), where itemView is view you inflated onCreateViewHolder. Have a look here. E.g.

 Picasso.with(viewHolder.itemView.getContext()).load(imageUrl).into(viewHolder.field_image_thumb);