如何将背景传递给毕加索

时间:2017-04-20 05:14:54

标签: android android-viewholder

您好我正在尝试使用Picasso添加图片,但是当我使用以下视图持有者时,在适配器中获取Picasso的上下文时遇到问题:

这是我的适配器

               public class DataAdapter extends 
                  RecyclerView.Adapter<DataAdapter.ViewHolder> {
                            private ArrayList<String> 
                              al_prod_name,al_prod_price,al_prod_id,
                                 al_prod_image;

                    public DataAdapter(ArrayList<String> al_prod_id, 
                           ArrayList<String> al_prod_name,
                               ArrayList<String> al_prod_price, 
                                  ArrayList<String> al_prod_image) {
                                   this.al_prod_id = al_prod_id;
                                   this.al_prod_price = al_prod_price;
                                   this.al_prod_name = al_prod_name;
                                   this.al_prod_image = al_prod_image
                               }


                      public DataAdapter.ViewHolder 
                         onCreateViewHolder(ViewGroup viewGroup, int i) {
                              View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardrow, viewGroup, 
                                    false);
                              return new ViewHolder(view);
                             }

                        public void onBindViewHolder(DataAdapter.ViewHolder 
                          viewHolder, int i) {

                             viewHolder.tv_prod_name.setText
                               (al_prod_name.get(i));
                             viewHolder.tv_prod_price.setText
                               (al_prod_price.get(i));

                        Picasso.with().load(al_prod_image.get(i))
                       .placeholder(R.drawable.floatingicon)
                       .error(R.drawable.floatingicon).
                       .into(viewHolder.img_prod);

                          }
                      public int getItemCount() {
                      return al_prod_id.size();
                      }

                   public class ViewHolder extends RecyclerView.ViewHolder{
                      private TextView tv_prod_name,tv_prod_price;
                      private ImageView img_prod;
                      public ViewHolder(View view) {
                      super(view);

                       tv_prod_name = (TextView)view
                       .findViewById(R.id.tv_prod_name);
                        tv_prod_price = (TextView)view
                       .findViewById(R.id.tv_prod_price);
                       img_prod = (ImageView)view
                       .findViewById(R.id.img_prod);
                       }
                       }
                       }

请帮助获取上下文。这就是我设置适配器的方式:

                       RecyclerView.Adapter adapter = new 
                       DataAdapter(al_prod_id,al_prod_name,
                       al_prod_price,al_prod_image);

7 个答案:

答案 0 :(得分:7)

最简单的解决方案是使用 viewHolder.itemView.getContext()

Picasso.with(viewHolder.itemView.getContext()).load(al_prod_image.get(i))
                   .placeholder(R.drawable.floatingicon)
                   .error(R.drawable.floatingicon).
                   .into(viewHolder.img_prod);

答案 1 :(得分:2)

将构造函数更改为

private Context context;
public DataAdapter(Context context, ArrayList<String> al_prod_id, 
                       ArrayList<String> al_prod_name,
                           ArrayList<String> al_prod_price, 
                              ArrayList<String> al_prod_image) {
                               this.context = context;
                               this.al_prod_id = al_prod_id;
                               this.al_prod_price = al_prod_price;
                               this.al_prod_name = al_prod_name;
                               this.al_prod_image = al_prod_image
                           }

现在将您的适配器称为

RecyclerView.Adapter adapter = new 
                   DataAdapter(context, al_prod_id,al_prod_name,
                   al_prod_price,al_prod_image);

此处上下文是您的活动参考。希望它会对你有所帮助。

答案 2 :(得分:2)

将您的活动上下文传递给DataAdapter

RecyclerView.Adapter adapter = new  DataAdapter(yourActivity.this,al_prod_id,al_prod_name,
                   al_prod_price,al_prod_image);

DataAdapter的

private Context mContext;
private ArrayList<String> al_prod_name,al_prod_price,al_prod_id,al_prod_image;

public DataAdapter(Context mContext,ArrayList<String> al_prod_id,  ArrayList<String> al_prod_name,ArrayList<String> al_prod_price, ArrayList<String> al_prod_image)
{
   this.mContext=mContext;
   this.al_prod_id = al_prod_id;
   this.al_prod_price = al_prod_price;
   this.al_prod_name = al_prod_name;
   this.al_prod_image = al_prod_image
}

//set image
  Picasso.with(mContext).load(al_prod_image.get(i))
                   .placeholder(R.drawable.floatingicon)
                   .error(R.drawable.floatingicon).
                   .into(viewHolder.img_prod);

答案 3 :(得分:1)

在适配器Context上下文中声明数据成员; 像这样编写适配器的构造函数,

DataAdapter(Context context, otherAurguments);

以及创建适配器时

DataAdapter adapter = new DataAdapter(yourActivityName.this , otherAurguments);

答案 4 :(得分:1)

每个视图都有一个上下文。我建议您使用2.27-SNAPSHOT来防止在活动上下文变为空时应用崩溃

getApplicationContext()

Picasso.with(itemView.getContext()).load(al_prod_image.get(i))

答案 5 :(得分:1)

来自活动,

 RecyclerView.Adapter adapter = new DataAdapter(YOUR_ACTIVITY.this, al_prod_id,al_prod_name, al_prod_price,al_prod_image);

在适配器中,

   private Context context;

public DataAdapter(Context mContext, ArrayList<String> al_prod_id, ArrayList<String> al_prod_name, ArrayList<String> al_prod_price, ArrayList<String> al_prod_image) {
     this.context= mContext;
     this.al_prod_id = al_prod_id;
     this.al_prod_price = al_prod_price;
     this.al_prod_name = al_prod_name;
     this.al_prod_image = al_prod_image
 }

将此与Picasso的上下文一起使用

Picasso.with(context).load(al_prod_image.get(i))
                   .placeholder(R.drawable.floatingicon)
                   .error(R.drawable.floatingicon).
                   .into(viewHolder.img_prod);

答案 6 :(得分:1)

像这样更改你的DataAdapter构造函数在你的适配器类中创建一个Context对象并像这样改变你的构造函数

 public DataAdapter(Context context,ArrayList<String> al_prod_id, 
                           ArrayList<String> al_prod_name,
                               ArrayList<String> al_prod_price, 
                                  ArrayList<String> al_prod_image) {
                                   this.al_prod_id = al_prod_id;
                                   this.al_prod_price = al_prod_price;
                                   this.al_prod_name = al_prod_name;
                                   this.al_prod_image = al_prod_image;
this.mContext = context;
                               }