Android - How can diplay pictures in a simpleApapter list view

筅森魡賤 提交于 2019-12-06 05:50:36

you can't "pass Picasso" to the adapter. You have to create your own custom adapter, it's not as daunting as it sounds. It may even be based on the SimpleAdapter. Like this:

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;
   }
}

BTW on Custom Adapter you can use it BY:

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

Then you can just use this class without the image on the parameters (but it must still exist inside 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);

For image size I think you need to re-size it !! as in example above .resize(imageWidth, imageWidth) hope this help!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!