java - Android-如何在简单的纸张 ListView 中显示图片

标签 java android android-listview picasso simpleadapter

我正在尝试在 SimpleAdapter ListView 中显示图片。我将 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

如何才能正确显示图像?

最佳答案

您无法将 picasso “传递给”适配器。您必须创建自己的自定义适配器,这并不像听起来那么令人畏惧。它甚至可能基于 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) 希望这有帮助!

关于java - Android-如何在简单的纸张 ListView 中显示图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28064264/

相关文章:

java - Dialog.dimiss() 不起作用

android - 如何在快速滚动期间禁用滑动刷新?

Android ListView 在滚动时崩溃

java - 从大型机批处理程序调用 Web 服务

javascript - 在 Java 中使用 Jsoup 检测 Javascript 库

通过 Imageview 进行 Android 手势检测

android - Android 设备的唯一 ID

java - 安卓 MySQL : JDBC CommunicationsException

android - Android 4 上的 Material 设计 ListView

java - 如何通过application.properties激活KafkaBatchListener (factory.setBatchListener(true))