【问题标题】:Android - How can diplay pictures in a simpleApapter list viewAndroid - 如何在简单的纸张列表视图中显示图片
【发布时间】:2015-01-21 09:59:46
【问题描述】:

我正在尝试在 SimpleAdapter 列表视图中显示图片。我在我的项目中加入了 Picasso,但我不知道如何将 Picasso 与 SimpleAdapter 一起使用。

List<HashMap<String, String>> featured = new ArrayList<HashMap<String, String>>();

            for (int i=0;i<ids.length;i++){
                HashMap<String, String> hm = new HashMap<String, String>();
                hm.put("productname", names[i]);
                hm.put("productprice", prices[i]);
                hm.put("productimage", images[i]);
                featured.add(hm);
            }

            String[] from = {"productname", "productprice", "productimage"};
            int[] to = {R.id.tv_ProductName, R.id.tv_ProductPrice, R.id.icon};

            SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, featured, R.layout.listitem_featured, from, to);
            HorizontalListView featuredList = (HorizontalListView) findViewById(R.id.lv_Featured);
            featuredList.setAdapter(adapter);

产品名称和价格显示正确。根本不显示产品图像。图片是url,例如:

http://thingd-media-ec1.thefancy.com/default/171510088920465761_1686d73fc160.jpeg

如何正确显示图像?

【问题讨论】:

    标签: java android android-listview picasso simpleadapter


    【解决方案1】:

    您不能将毕加索“传递”给适配器。您必须创建自己的自定义适配器,这并不像听起来那么令人生畏。它甚至可能基于SimpleAdapter。像这样:

    public class MyAdapter extends SimpleAdapter{
    
      public MyAdapter(Context context, List<? extends Map<String, ?>> data, int     resource, String[] from, int[] to){
      super(context, data, resource, from, to);
      }
    
       public View getView(int position, View convertView, ViewGroup parent){
      // here you let SimpleAdapter built the view normally.
      View v = super.getView(position, convertView, parent);
    
       // Then we get reference for Picasso
      ImageView img = (ImageView) v.getTag();
      if(img == null){
         img = (ImageView) v.findViewById(R.id.imageOrders);
         v.setTag(img); // <<< THIS LINE !!!!
      }
      // get the url from the data you passed to the `Map`
      String url = ((Map)getItem(position)).get(TAG_IMAGE);
      // do Picasso
      // maybe you could do that by using many ways to start
    
        Picasso.with(context).load(url)
                .resize(imageWidth, imageWidth).into(img);
    
      // return the view
      return v;
       }
    }
    

    顺便说一句,您可以在自定义适配器上使用它:

    Picasso.with(context).load(yoururl)
                .resize(imageWidth, imageWidth).into(holder.imageItem);
    

    那么你可以直接使用这个类而不带参数上的图片(但它必须仍然存在于 orderList 中)。

    ListView list= (ListView) getActivity().findViewById(R.id.list);
    ListAdapter adapter = 
       new MyAdapter(
                getActivity(),
                orderList,
                R.layout.order_usa_row,
                new String[]{TAG_PRICE,TAG_TITLE,TAG_PSTATUS,TAG_PRICESYMBOL},
                new int[]{R.id.price,R.id.title,R.id.pstatus,R.id.symbol});
     list.setAdapter(adapter);
    

    对于图像大小,我认为您需要重新调整大小!如上面的例子.resize(imageWidth, imageWidth)希望这有帮助!

    【讨论】:

    • 如何定义和使用 TAG_NAME、TAG_PRICE 和 TAG_IMAGE?也许是一个愚蠢的问题......
    • 不......当然没有问题愚蠢但未问的问题是一个!无论如何,它只是示例,TAG_NAME、TAG_PRICE 和 TAG_IMAGE 将图像位置称为 ex 等。您可以根据代码需要使用它们!
    • 好的,我明白了!但我有这个由 AsyncTask 从 php 服务器检索的 url 数组。一个字符串是这样声明的:private static final String TAG_SUCCESS = "success";我如何声明一个数组?
    • mm 您需要根据您的要求更改 MyAdapter 参数,其中包含产品名称、价格和图像路径的数组!正如我告诉你的,就像你如何使用毕加索一样......
    • 对不起,我真的不明白。你能用我的一段代码给我看一个例子吗?我不明白如何获取 url 数组 images[] 并在 MyAdapter 类中使用它们?我知道它需要在这里检索网址String url = ((Map)getItem(position)).get(.....);
    猜你喜欢
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多