毕加索图书馆在android中的RecyclerView中不工作

时间:2015-07-10 01:45:01

标签: java android picasso

所以我在我的适配器中遇到这个问题,当我去设置Picasso方法转换我的图像网址时,无论我怎么做,它都不允许我传递上下文。我试过这个和班级名字。这似乎都不起作用。不知道发生了什么或为什么会发生这种情况。这是我遇到问题的适配器类。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ContentViewHolder> {



public  content[] mDataset;

public MyAdapter(content[] data) {
    mDataset = data;

}


@Override
public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.test, parent, false);
    ContentViewHolder viewHolder = new ContentViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(ContentViewHolder holder, int position) {
    holder.bindContent(mDataset[position]);
}

@Override
public int getItemCount() {
    return mDataset.length;
}

public class ContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView mUrl;
    public TextView mTitle;
    public TextView mDate;
    public TextView mAuthor;
    public ImageView mThumbnail;

    public ContentViewHolder(View itemView) {
        super(itemView);
        mUrl= (TextView) itemView.findViewById(R.id.url);
        mTitle = (TextView) itemView.findViewById(R.id.title);
        mDate = (TextView) itemView.findViewById(R.id.date);
        mAuthor = (TextView) itemView.findViewById(R.id.author);
        mThumbnail =(ImageView)itemView.findViewById(R.id.thumbnail);

    }

    public void bindContent(content bloginfo) {
        mUrl.setText(bloginfo.getUrl());
        mTitle.setText(bloginfo.getTitle());
        mDate.setText(bloginfo.getDate());
        mAuthor.setText(bloginfo.getAuthor());


        Picasso.with(context).load(bloginfo.getThumbnail()).into(mThumbnail);

    }

    @Override
    public void onClick(View view) {

    }
}
}

4 个答案:

答案 0 :(得分:2)

好的,所以我在这里工作的是下面的工作适配器代码。谢谢大家的帮助。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ContentViewHolder> {

public  content[] mDataset;
private Activity activityContext;

public MyAdapter(Activity context,content[] data) {
    mDataset = data;
    activityContext = context;
}

@Override
public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.test, parent, false);
    ContentViewHolder viewHolder = new ContentViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(ContentViewHolder holder, int position) {
    holder.bindContent(mDataset[position]);
}

@Override
public int getItemCount() {
    return mDataset.length;
}

public class ContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView mUrl;
    public TextView mTitle;
    public TextView mDate;
    public TextView mAuthor;
    public ImageView mThumbnail;

    public ContentViewHolder(View itemView) {
        super(itemView);
        mUrl= (TextView) itemView.findViewById(R.id.url);
        mTitle = (TextView) itemView.findViewById(R.id.title);
        mDate = (TextView) itemView.findViewById(R.id.date);
        mAuthor = (TextView) itemView.findViewById(R.id.author);
        mThumbnail =(ImageView)itemView.findViewById(R.id.thumbnail);

    }

    public void bindContent(content bloginfo) {
        mUrl.setText(bloginfo.getUrl());
        mTitle.setText(bloginfo.getTitle());
        mDate.setText(bloginfo.getDate());
        mAuthor.setText(bloginfo.getAuthor());


        Picasso.with(activityContext).load(bloginfo.getThumbnail()).into(mThumbnail);

    }

    @Override
    public void onClick(View view) {

    }
  }
}

答案 1 :(得分:0)

 Picasso.with(MyAdapter.this)

应该是

 Picasso.with(context)

使用活动上下文。这可以从activity传递给适配器类的构造函数。

http://developer.android.com/reference/android/content/Context.html

http://square.github.io/picasso/

github上提供的样本

https://github.com/square/picasso/blob/master/picasso-sample/src/main/java/com/example/picasso/PicassoSampleAdapter.java

答案 2 :(得分:0)

在您的适配器中,您可以:

private Activity activityContext;

然后在Adapter contructor中:

public MyAdapter(Activity context, content[] data) {
    mDataset = data;
    activityContext = context;
}

最后致电:

Picasso.with(activityContext).load(bloginfo.getThumbnail()).into(mThumbnail);

如果您在Activity中创建适配器,则需要使用以下命令创建它: new myAdapter(this, YOURDATA),如果您在Fragment中创建它,则需要使用new myAdapter(getActivity(), YOURDATA)

答案 3 :(得分:0)

Picasso.with(mThumbnail.getContext()).load(bloginfo.getThumbnail()).into(mThumbnail);