我正在使用此自定义适配器来显示 youtube 缩略图。
public class MyMedyaAdapter extends SimpleAdapter {
Context context;
int layout;
List<? extends Map<String, ?>> data;
String[] from;
int[] to;
String resimURL = "http://img.youtube.com/vi/{0}/0.jpg";
public MyMedyaAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.context = context;
this.layout = resource;
this.data = data;
this.from = from;
this.to = to;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(layout, parent, false);
TextView metin = (TextView) rowView.findViewById(R.id.row_medya_text);
String baslik = data.get(position).get(from[0]) + "";
baslik = baslik.replace(".pdf", "");
metin.setText(baslik);
if (from.length > 1) {
final ImageView resim = (ImageView) rowView.findViewById(R.id.row_medya_image);
final int p2 = position;
String resimID = data.get(p2).get(from[1])+"";
String rowImage = resimURL.replace("{0}", resimID);
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(rowImage).getContent());
resim.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO: handle exception
}
}
return rowView;
}
}
使用此代码将适配器添加到列表视图;
public class VideoThered extends AsyncTask<Void, Void, MyMedyaAdapter> {
@Override
protected MyMedyaAdapter doInBackground(Void... params) {
List<HashMap<String, String>> response = new ArrayList<HashMap<String, String>>();
try {
//JSON request
response = new JsonHelper().GetVideoEntitys();
} catch (Exception e) {
response = null;
}
if (response != null) {
adapter = new MyMedyaAdapter(ctx, response, R.layout.row_medya, from, to);
}
else
{
adapter = null;
}
return adapter;
}
@Override
protected void onPostExecute(MyMedyaAdapter result) {
if (result != null) {
//StrictMode$AndroidBlockGuardPolicy.onNetwork() error with this line on honeycomb
listVideolar.setAdapter(adapter);
}
}
}
我收到 /StrictMode$AndroidBlockGuardPolicy.onNetwork() 错误。
我怎样才能设法显示来自网络的行图像而不出现此错误?
我正在使用来自 AsyncTask 的列表视图主要数据。在这个数据列表中,每个项目都有一个唯一的 youtube 视频 ID。使用此 ID,我正在从网络上获取视频缩略图。但是在我的自定义适配器上没有使用 AsyncTask。我知道这就是问题所在。但是我怎样才能管理我的自定义适配器来使用 AsyncTask?