I've been looking around to find a way to do Picasso in a separate thread as it is loading it from the internet. I have no idea how to confirm if it is.
My app is basically a ListView with one imageView and two textViews and I have created a CustomAdapter which extends BaseAdapter to assist the List. This is what I have done in my adapter class:
public class CustomAdapter extends BaseAdapter
{
public String title[];
public String description[];
public String images[]; //Image URLs
private static Picasso instance;
public Activity context;
public LayoutInflater inflater;
public CustomAdapter(Activity context, String[] title, String[] description, String[] images) {
super();
this.context = context;
this.title = title;
this.description = description;
this.images = images;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static Picasso getSharedInstance(Context context)
{
if(instance == null)
{
instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();
return instance;
}
else
{
return instance;
}
}
public static class ViewHolder
{
ImageView icon;
TextView itemNameTextView;
TextView itemDescriptionTextView;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
Debug.startMethodTracing("ThreadCheck");
ViewHolder holder;
if(view==null)
{
holder = new ViewHolder();
view = inflater.inflate(R.layout.list_item, null);
holder.icon = (ImageView) view.findViewById(R.id.DownloadedImage);
holder.itemNameTextView = (TextView) view.findViewById(R.id.ItemNameTextView);
holder.itemDescriptionTextView = (TextView) view.findViewById(R.id.ItemDescriptionTextView);
view.setTag(holder);
}
else
holder=(ViewHolder)view.getTag();
Picasso.with(context).load(images[position]).into(holder.icon);
holder.itemNameTextView.setText(title[position]);
holder.itemDescriptionTextView.setText(description[position]);
return view;
}
}
What I wanted to know was:
1) Does the following make Picasso to load the image in a seperate thread? (I read this somewhere and it didn't really make sense to me and I'm hoping you guys would explain it to me.)
instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();
2) Or does
Picasso.with(context).load(images[position]).into(holder.icon);
automatically do it in a new thread?
3) Or does neither of these do it in a new thread and I need to do it seperately? If yes, how so?
Thanks in advance!
Picasso
downloads images from the Internet in a background thread by default. That's why we use such libraries for image loading.