URLから画像を取得し、それらをグリッドビューにロードする必要があります。Base Adapter を拡張する ImageAdapter クラスの getView メソッドで、ネットワーク オン メイン スレッドの例外を取得しています。別のスレッドまたは非同期タスクを使用してこれを解決するにはどうすればよいですか。
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
try {
URL url = new URL(Product_ItemsURLs[position]);
InputStream content = (InputStream)url.getContent();
Drawable drawable = Drawable.createFromStream(content , "src");
if (convertView == null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(380,380));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(10, 10, 10, 10);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageDrawable(drawable);
return imageView;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}