【问题标题】:Display Images From Array of String URL's in ListView in Android Studio using Picasso使用 Picasso 在 Android Studio 的 ListView 中显示字符串 URL 数组中的图像
【发布时间】:2016-11-10 09:29:26
【问题描述】:

我正在尝试使用毕加索加载包含文本和图像的列表视图。我正在尝试使用字符串数组中各自的 URL 加载图像。文本部分正在列表视图中加载。但是,图像没有加载,所以我得到的只是一个 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);
    }
}

【问题讨论】:

  • return super.getView(position, convertView, parent); 为什么?它使您的整个方法的代码无用
  • @Selvin 那我该怎么办?返回转换视图?
  • 那我应该怎么做?阅读有关适配器类的基础知识......显然Adapter.getView 应该:1. 如果convertView 为空,则创建视图 2. 将数据绑定到视图(从 convertView 创建或回收) 3. 返回创建(或回收)视图 ...

标签: android listview picasso


【解决方案1】:

我已经修改了你的适配器。

........
@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);
 }
}

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-03
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多