Search code examples
javaandroidandroid-recyclerviewpicassoandroid-context

Picasso - can't find "context"


First of all, I am a beginner and this may be a stupid question for you. I need to pass context to the following code in order to load an image in the recyclerview. Tried several options but any of them is not a success. My problem is how to find the context that to pass in the following code.

Picasso.with(context here).load("http://i.imgur.com/DvpvklR.png").into(thumbnail);

Here is the full code which causes the problem(I am trying to pass an image to an recyclerview)

public class WishListAdapter extends RecyclerView.Adapter<WishListAdapter.DerpHolder> {

    private List<WishListItem> listData;
    private LayoutInflater inflater;

    public WishListAdapter(List<WishListItem> listData, Context c) {
        inflater = LayoutInflater.from(c);
        this.listData = listData;
    }

    @Override
    public WishListAdapter.DerpHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.wish_list_item, parent, false);
        return new DerpHolder(view);
    }

    @Override
    public void onBindViewHolder(DerpHolder holder, int position) {

    }

    public void setListData(ArrayList<WishListItem> exerciseList) {
        this.listData.clear();
        this.listData.addAll(exerciseList);
    }

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

    class DerpHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView thumbnail;
        TextView title;
        TextView subTitle;
        View container;


        public DerpHolder(View itemView) {
            super(itemView);
            thumbnail = itemView.findViewById(R.id.im_item_icon);

            //subTitle = itemView.findViewById(R.id.lbl_item_sub_title);

            //title = itemView.findViewById(R.id.lbl_item_text);

            container = itemView.findViewById(R.id.cont_item_root);
            container.setOnClickListener(this);
            Picasso.with(context ).load("http://i.imgur.com/DvpvklR.png").into(thumbnail);
        }

        @Override
        public void onClick(View v) {
            Log.d("janitha", "item clicked");

        }

Also I have read following too:-Picasso and context

Expect help from somebody.


Solution

  • You haven't saved context from your constructor. Do as like as this:

    public class WishListAdapter extends 
         RecyclerView.Adapter<WishListAdapter.DerpHolder> {
    
        private List<WishListItem> listData;
        private LayoutInflater inflater;
        Context context;
    
        public WishListAdapter(List<WishListItem> listData, Context c) {
            inflater = LayoutInflater.from(c);
            this.listData = listData;
            this.context = c;
        }
    
        @Override
        public WishListAdapter.DerpHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.wish_list_item, parent, false);
            return new DerpHolder(view);
        }
    
        @Override
        public void onBindViewHolder(DerpHolder holder, int position) {
    
        }
    
        public void setListData(ArrayList<WishListItem> exerciseList) {
            this.listData.clear();
            this.listData.addAll(exerciseList);
        }
    
        @Override
        public int getItemCount() {
            return listData.size();
        }
    
        class DerpHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    
            ImageView thumbnail;
            TextView title;
            TextView subTitle;
            View container;
    
    
            public DerpHolder(View itemView) {
                super(itemView);
                thumbnail = itemView.findViewById(R.id.im_item_icon);
    
                //subTitle = itemView.findViewById(R.id.lbl_item_sub_title);
    
                //title = itemView.findViewById(R.id.lbl_item_text);
    
                container = itemView.findViewById(R.id.cont_item_root);
                container.setOnClickListener(this);
                Picasso.with(context ).load("http://i.imgur.com/DvpvklR.png").into(thumbnail);
            }
    
            @Override
            public void onClick(View v) {
                Log.d("janitha", "item clicked");
    
            }