我创建了我的应用程序,它连接到我服务器上的JSON文件以获取有关用户的信息,然后将其设置到卡行上。这一切都运行正常但我不能让Picasso使用它,除非我将它放在我的公共ViewHolder(视图)方法中,但后来我无法访问该数组。有没有什么办法解决这一问题?我收到了错误
错误:(55,22)错误:不兼容的类型:DataAdapter.ViewHolder 无法转换为上下文
代码发布在下面。感谢。
package com.example.curtisboylan.myapplication;
/**
* Created by curtisboylan on 02/02/2017.
*/
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<String> username;
private ArrayList<String> userid;
private ArrayList<String> location;
private ArrayList<String> reviewscore;
private ArrayList<String> price;
private ArrayList<String> url;
public DataAdapter(ArrayList<String> username, ArrayList<String> userid, ArrayList<String> location, ArrayList<String> reviewscore, ArrayList<String> price, ArrayList<String> url) {
this.username = username;
this.userid = userid;
this.location = location;
this.reviewscore = reviewscore;
this.price = price;
this.url = url;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
final String item = userid.get(i);
final String usernamepass = username.get(i);
viewHolder.usernametext.setText(username.get(i));
viewHolder.reviewscoretext.setText(reviewscore.get(i));
viewHolder.pricetext.setText("€" + price.get(i) +"/hr");
viewHolder.locationtext.setText(location.get(i));
//"https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg";
Picasso.with(viewHolder)
.load(url.get(i))
.into(viewHolder.profileimg);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "User ID: " + item, Toast.LENGTH_SHORT).show();
Intent intent=new Intent(view.getContext(),TechnicianProfile.class);
intent.putExtra("username", usernamepass);
intent.putExtra("userid", item);
view.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return username.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView usernametext;
private TextView pricetext;
private TextView locationtext;
private TextView reviewscoretext;
private ImageView profileimg;
public ViewHolder(View view) {
super(view);
usernametext = (TextView)view.findViewById(R.id.textView4);
pricetext = (TextView)view.findViewById(R.id.price);
locationtext = (TextView)view.findViewById(R.id.location);
reviewscoretext = (TextView)view.findViewById(R.id.reviewscore);
profileimg = (ImageView)view.findViewById(R.id.userpic);
}
}
}
答案 0 :(得分:1)
您应该通过 Context
新的DataAdapter(..,..,CurrentActivity.this) //传递上下文
Context context;//Global
public DataAdapter(ArrayList<String> username, ArrayList<String> userid, ArrayList<String> location, ArrayList<String> reviewscore, ArrayList<String> price, ArrayList<String> url,Context context) {
this.context = context;
}
现在
Picasso.with(context)
答案 1 :(得分:0)
Picasso.with(viewHolder.itemView.getContext())
...
但这很容易出错,你最好给application context以逃避泄密。
答案 2 :(得分:0)
毕加索需要一个Context对象
Picasso.with(context).load("http://url/image.png").into(imageView);
您可以做的是通过适配器的构造函数传递Context对象:
Context context;
public DataAdapter(Context context, ..) {
this.context = context;
...
}
并最终从调用适配器的活动中传递该上下文对象:
adapter = DataAdapter(getContext(), ...);
答案 3 :(得分:0)
活动有自己的上下文。但适配器没有。所以你需要使用构造函数将上下文从Activity传递给Adapter,你必须将这个上下文传递给Picasso加载器。
Picasso.with(context)
.load(url.get(i))
.into(viewHolder.profileimg);
将 viewHolder 替换为您通过构造函数获得的上下文。