我如何毕加索传递到ListView的适配器 [英] How do i pass picasso into listView adapter

查看:136
本文介绍了我如何毕加索传递到ListView的适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这条线传递到列表中的适配器

  Picasso.with(getActivity())负载(图片网址).into(imageOrders)。


  

列表


 的ListView列表=(ListView控件)getActivity()findViewById(R.id.list)。
ListAdapter适配器=新SimpleAdapter(getActivity(),orderList,R.layout.order_usa_row,
新的String [] {TAG_PRICE,TAG_TITLE,TAG_PSTATUS,TAG_PRICESYMBOL,TAG_IMAGE},新的INT [] {R.id.price,R.id.title,R.id.pstatus,R.id.symbol,R.id.imageOrders });list.setAdapter(适配器);

我是一个初学者,我尝试了很多,但我不出来,请大家帮忙


解决方案

您不能毕加索传到适配器。你必须创建自己的自定义适配器,它并不像听起来那么令人望而生畏。它甚至可以基于该SimpleAdapter。像这样的:

 公共类MyAdapter扩展SimpleAdapter {   公共MyAdapter(上下文的背景下,列表与LT ;?扩展地图<字符串,>>的数据,INT资源的String []从,INT []到){
      超(背景下,数据,资源,从,到);
}   公共查看getView(INT位置,查看convertView,父母的ViewGroup){
      //在这里让SimpleAdapter建立视图正常。
      视图V = super.getView(位置,convertView,父母);      //然后我们得到毕加索参考
      ImageView的IMG =(ImageView的)v.getTag();
      如果(IMG == NULL){
         IMG =(ImageView的)v.findViewById(R.id.imageOrders);
         v.setTag(IMG); //<<<此行!!!!
      }
      //得到你传递给`Map`数据的网址
      字符串URL =((图)的getItem(位置))获得(TAG_IMAGE)。
      //做毕加索
      。Picasso.with(v.getContext())负载(URL).into(IMG);      //返回视图
      返回伏;
   }
}

那么你可以使用这个类不会对参数图片(但它仍然必须内部存在 orderList )。

 的ListView列表=(ListView控件)getActivity()findViewById(R.id.list)。
ListAdapter适配器=
       新MyAdapter(
                getActivity(),
                orderList,
                R.layout.order_usa_row,
                新的String [] {TAG_PRICE,TAG_TITLE,TAG_PSTATUS,TAG_PRICESYMBOL},
                新的INT [] {R.id.price,R.id.title,R.id.pstatus,R.id.symbol});
list.setAdapter(适配器);

i need to pass this line into the list adapter

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

List

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

i'm a begginer, i tried a lot but i can't figure it out , please help

解决方案

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

      // return the view
      return v;
   }
}

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

这篇关于我如何毕加索传递到ListView的适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆