将上下文传递给RecyclerViewAdapter

时间:2015-06-30 22:14:37

标签: android

我刚开始学习android编程,我正在尝试将RSS源中的Urls加载到我的回收站视图中(我已经完成了)。我自己设置的下一个任务是将RSS提要中的图像加载到RecyclerView中,但是我在尝试传递Picasso插件(http://square.github.io/picasso/)的上下文时遇到了问题,甚至为什么这样做/ getContext / getApplicationContext不起作用。

这是我的适配器代码,其中包含Picasso插件的行。

任何帮助/理解都将不胜感激。

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.ApplicationViewHolder> {


public static class ApplicationViewHolder extends RecyclerView.ViewHolder {

    CardView cv;
    TextView personName;
    TextView personAge;
    TextView personReleasedate;
    ImageView imageUrl;

    ApplicationViewHolder(View itemView) {
        super(itemView);

        cv = (CardView) itemView.findViewById(R.id.cv);
        personName = (TextView) itemView.findViewById(R.id.person_name);
        personAge = (TextView) itemView.findViewById(R.id.person_age);
        personReleasedate = (TextView) itemView.findViewById(R.id.personReleaseDate);
        imageUrl = (ImageView) itemView.findViewById(R.id.postImageView);
    }
}

List<Application> applications;

RVAdapter(List<Application> applications) {
    this.applications = applications;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

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

@Override
public void onBindViewHolder(ApplicationViewHolder applicationViewHolder, int i) {
    applicationViewHolder.personName.setText(applications.get(i).getName());
    applicationViewHolder.personAge.setText(applications.get(i).getArtist());
    applicationViewHolder.personReleasedate.setText(applications.get(i).getReleaseDate());
    Log.d("RVADAPTER", applications.get(i).getImageURL());
    Picasso.with(this).load(applications.get(i).getImageURL()).into(applicationViewHolder.imageUrl);
  //  applicationViewHolder.imageUrl.setImageBitmap(Picasso.with(context).load(applications.get(i).getImageURL()));

// applicationViewHolder.imageUrl.setText(applications.get(i).getImageURL());

}

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

}

1 个答案:

答案 0 :(得分:0)

添加

List<Application> applications;
Context context;

RVAdapter(List<Application> applications, Context context) {
    this.applications = applications;
    this.context = context;
}

并从activity / fragment传递它?