我想使用Picasso从json url获取图像,但不知道如何在RecyclerView中使用。 的 Picasso.with(上下文).load(将String.valueOf(users.get(位置)))代入(holder.avatar);
我在使用方法得到错误时如何定义上下文???
UserAdapter.java
TimePickerDialog
答案 0 :(得分:1)
使用此
private Context context;
private ArrayList<User> users = new ArrayList<>();
private LayoutInflater layoutInflater;
//constructor
public UserAdapter(ArrayList<User> users, Context context) {
this.users = users;
this.context = context;
this.layoutInflater=layoutInfalter.from(context);
}
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInfalter.inflate(R.layout.items,parent,false);
return new UserViewHolder(view);
}
@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
holder.login.setText(users.get(position).getUser_login());
holder.type.setText(users.get(position).getType());
Picasso.with(context).
load(String.valueOf(users.get(position)))
.into(holder.avatar);
}
@Override
public int getItemCount() {
return users.size();
}
public class UserViewHolder extends RecyclerView.ViewHolder {
TextView login;
TextView type;
ImageView avatar;
public UserViewHolder(View itemView) {
super(itemView);
login = (TextView)itemView.findViewById(R.id.login);
type = (TextView)itemView.findViewById(R.id.type);
avatar = (ImageView)itemView.findViewById(R.id.avatar);
}
}
答案 1 :(得分:0)
尝试从activity
或fragment
Context context;
public UserAdapter(ArrayList<User> users, Context context) {
this.users = users;
this.context = context;
}
像这样叫你适配器,
UserAdapter adapter = new UserAdapter(usermodel, context);
此处的上下文将activity context
或fragment context
或getApllicationContext()
现在,您可以调用Picasso从服务器Picasso.with(context).load(String.valueOf(users.get(position))).into(holder.avatar);