Enter Picaso lib into a listview without an adapter

0

Good, I'm doing a query to an array json, everything works wonders, the code is simple and light, the problem I get when I want to add an image to my listview, since it does not show me.

I have this code snippet:

    ListAdapter adapter = new SimpleAdapter(
                        tab_bancos.this, BancoList,
                        R.layout.lista_bancos, new String[]{"logo","bank", "accholder",
                        "id", "account"}, new int[]{
                        R.id.bankimg,R.id.bankname,
                        R.id.bankacch, R.id.bankaccid, R.id.accnumber});

                lv.setAdapter(adapter);

I want to know if I can use picasso to enter the image, in other tests this code has worked well for me:

    pruebitalogo = (CircleImageView)view.findViewById(R.id.pruebitaimg);
    Picasso.with(getActivity()).load("http://www.asturscore.com/wp-content/uploads/2017/02/Logan.jpeg").into(R.id.pruebitalogo);

Now, what I want is to replace in my code "logo" and that I accept the url and fill the object R.id.bankimg

    
asked by Jesus Moran 10.09.2017 в 03:33
source

1 answer

0

I found the answer after investigating a bit, here I leave it in case someone has the same question:

You can not use picasso directly on the adapter, so you only have to modify it, create a class called MyAdapter that extends from 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;
   }
}

And then we call our adapter:

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

Here is the link where I get the answer, it is in English: link

    
answered by 10.09.2017 / 04:26
source