0
ピカソを別のスレッドでインターネットからロードする方法を探していました。私はそれがあるかどうかを確認する方法は分かりません。ピカソ画像マルチスレッドで読み込んでいますか?
私のアプリは基本的に1つのimageViewと2つのtextViewsを持つListViewであり、Listを補助するBaseAdapterを拡張したCustomAdapterを作成しました。これは私が私のアダプタクラスでやっていることです:私は知りたいと思った何
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;
}
}
されました:
1)以下は、別々のスレッドで画像をロードするためにピカソを作るのか? (私はこのどこを読んで、それは本当に私には意味がありませんでしたし、私はあなたたちは私にそれを説明するだろう願っています。)
instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();
2)または
Picasso.with(context).load(images[position]).into(holder.icon);
が自動的に行いますかそれは新しいスレッドで?
3)また、これらのどちらも新しいスレッドで実行しないため、別に行う必要がありますか?はいの場合、どうですか?
ありがとうございます!
「Picasso」は、デフォルトでバックグラウンドスレッドでインターネットから画像をダウンロードします。あなたはそれについて何を確認したいですか? –
はい、画像読み込みにUIスレッドの代わりにスレッドを使用します。 – anhtuannd
Picassoは、デフォルトのコンテキストとビルダーを使用してバックグラウンドスレッドでダウンロードします...あなたはNetworkOnMainThreadExceptionを取得しません。 – Kushan