Abc
Abc

Reputation: 19

How can i insert picasso into an adapter

I'm building an android application with a listview, and i would like to put images from different URL's into my listview. I'm getting the images from my Website server, just like this.

www.mywebsite.com/image1.jpg

The name of the image and the extension are located inside a json file.

I heard that Picasso do exactly what i want. And I just need to do that:

//Initialize ImageView
ImageView imageView = (ImageView) findViewById(R.id.imageView);

//Loading image from below url into imageView

Picasso.with(this)
   .load("URL HERE")
   .into(imageView);

But, I'm passing the data to a an adapter, and I dont know how can i adapt the Picasso code to my adapter code.

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    // Dismiss the progress dialog
    if (pDialog.isShowing())
        pDialog.dismiss();
    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(
            ListUsersActivity.this, contactList,
            R.layout.list_row, new String[] { TAG_NAME }, new int[] { R.id.name,
                    });

    setListAdapter(adapter);
}

As you can see, I'm already passing the user's name to a TextView called name, now, I would like to pass the PICTURE_NAME to an ImageView called avatar. How can i do that?

Thank you.

Upvotes: 1

Views: 4872

Answers (1)

Anitha
Anitha

Reputation: 253

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

Upvotes: 3

Related Questions