使用Picasso在Android Studio的ListView中显示来自String URL的数组图像

时间:2016-07-07 22:05:52

标签: android listview picasso

我正在尝试使用picasso加载包含文本和图像的列表视图。我正在尝试使用字符串数组中各自的URL加载图像。文本部分正在listView中加载。但是,图像没有加载所以我得到的是一个listView,其中各个行中的文本但没有图像。我怎么能用毕加索做到这一点?提前致谢!

class MyAdapter extends ArrayAdapter <String> {
    Context context;
    String[] descriptionArray;
    String[] url;
    MyAdapter (Context c, String[] importerArray,String[] url) {
        super(c, R.layout.content_orders,R.id.textView2, importerArray);
        this.context =c;
        this.descriptionArray = importerArray;
        this.url = url;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.content_orders, list, false);
        ImageView myImage = (ImageView) row.findViewById(R.id.imageView2);
        TextView description = (TextView) row.findViewById(R.id.textView2);
        description.setText(descriptionArray[position]);
        Picasso.with(OrdersActivity.this).load(url[position]).into(myImage);
        return super.getView(position, convertView, parent);
    }
}

1 个答案:

答案 0 :(得分:0)

我修改了你的适配器。

........
@Override
public View getView(int _position, View _convertView, ViewGroup _parent) {
View view = _convertView;
ViewHolder viewHolder;

if (view == null) {
    view= View.inflate(context, R.layout.content_orders, null);
    viewHolder = new ViewHolder(view);
    view.setTag(viewHolder);
} else {
    viewHolder = (ViewHolder) _convertView.getTag();
}
    viewHolder.description.setText(descriptionArray[position]);
    Picasso.with(OrdersActivity.this).load(url[position].replace(" ","%20")).into(viewHolder.myImage);  //here i'm editting your url, because if your url contains and blank space you can't download image from url
    //for that i'm encoding your URL by replacing blank space with "%20"
return view;
}
 .....
 .....
static class ViewHolder{
 ImageView myImage;
 TextView description;
 public ViewHolder(View v){
    myImage = (ImageView) v.findViewById(R.id.imageView2);
    description = (TextView) v.findViewById(R.id.textView2);
 }
}

希望这会对你有所帮助。