使用Glide加载RecyclerView图像

时间:2017-01-09 16:55:02

标签: android android-recyclerview android-glide

使用Glide在RecyclerView中加载图片时遇到问题。当我使用url时它可以工作但是当我从数组中获取url时它不起作用。

public class HistorialAdapter extends RecyclerView.Adapter<HistorialAdapter.HistorialViewHolder> {
private List<Incidencia> items;
private MenuActivity context;
private View v;
private String url;

public static class HistorialViewHolder extends RecyclerView.ViewHolder {
    public ImageView imagen;
    public TextView nombre;
    public TextView estado;
    public TextView fecha;
    public View v;


    public HistorialViewHolder(View v) {
        super(v);
        this.v=v;
        imagen = (ImageView) v.findViewById(R.id.imagen);
        nombre = (TextView) v.findViewById(R.id.titulo);
        estado = (TextView) v.findViewById(R.id.estado);
        fecha = (TextView) v.findViewById(R.id.date);
    }

}

public HistorialAdapter(Context context, List<Incidencia> items) {
    this.context = (MenuActivity) context;
    this.items = items;
}

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

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


    @Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}


@Override
public void onBindViewHolder(final HistorialViewHolder viewHolder, int pos)      {
    Log.v("url ", items.get(pos).getImage());
    url="http://thumbs.subefotos.com/0f309f78c5dc93515bf95c984728269do.jpg";
    Glide.with(context.getApplicationContext())
            //.load(url) -> it works
            .load(items.get(pos).getImage()) ->it doesn't work
            .asBitmap()
            .into(new BitmapImageViewTarget(viewHolder.imagen) {
                @Override
                protected void setResource(Bitmap resource) {
                    super.setResource(resource);
                }
            });
    viewHolder.nombre.setText(items.get(pos).getTitle());
    viewHolder.estado.setText(items.get(pos).getStatus());
    viewHolder.fecha.setText(items.get(pos).getFecha());

    ....More code...

}

这是网址日志

V/url: http://thumbs.subefotos.com/026b968faedb576d0675a3e17bbf9fe6o.jpg
V/url: http://thumbs.subefotos.com/0f309f78c5dc93515bf95c984728269do.jpg
V/url: http://thumbs.subefotos.com/026b968faedb576d0675a3e17bbf9fe6o.jpg
V/url: http://thumbs.subefotos.com/0f309f78c5dc93515bf95c984728269do.jpg

如何才能看到我的列表中有正确的网址?

1 个答案:

答案 0 :(得分:0)

我在调试时使用trim在我的url中解决了我得到这个字符串:

"http://thumbs.subefotos.com/026b968faedb576d0675a3e17bbf9fe6o.jpg            "

这就是问题