毕加索 - 无法找到"背景"

时间:2017-11-14 06:24:03

标签: java android android-recyclerview picasso android-context

首先,我是初学者,这对你来说可能是一个愚蠢的问题。 我需要将上下文传递给以下代码,以便在recyclerview中加载图像。尝试了几个选项,但其中任何一个都不成功。我的问题是如何找到传递以下代码的上下文。

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

以下是导致问题的完整代码(我正在尝试将图像传递给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");

        }

我也读过以下内容: - Picasso and context

期待某人的帮助。

4 个答案:

答案 0 :(得分:1)

您尚未从构造函数中保存上下文。像这样做:

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");

        }

答案 1 :(得分:1)

尝试,

private Context context;

并在您的构造函数中

this.context = c;

现在你可以使用'context'作为参数。

答案 2 :(得分:1)

修改您的代码如下:

private List<WishListItem> listData;
private LayoutInflater inflater;
private Context mContext;

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

然后将mContext Picasso

一起使用
Picasso.with(mContext).load("http://i.imgur.com/DvpvklR.png").into(thumbnail);

答案 3 :(得分:0)

请声明如下的上下文变量并使用它:

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

    private List<WishListItem> listData;
    private LayoutInflater inflater;
    private 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");

        }