我如何将毕加索传递给listView适配器

时间:2014-09-30 10:16:46

标签: android android-listview picasso

我需要将此行传递到列表适配器

Picasso.with(getActivity()).load(imageurl).into(imageOrders);
  

列表

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

list.setAdapter(adapter);

我是一个乞丐,我尝试了很多,但我无法弄清楚,请帮助

1 个答案:

答案 0 :(得分:5)

你不能“将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
      Picasso.with(v.getContext()).load(url).into(img);

      // return the view
      return v;
   }
}

然后你可以使用这个没有参数图像的类(但它必须仍然存在于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);