©我正在使用此自定義適配器來顯示youtube縮略圖。自定義ListView適配器使用URL圖像獲取錯誤'StrictMode'
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的listview主數據。 在這個數據列表中,每個項目都有唯一的YouTube視頻ID。與此ID我從網絡獲取視頻縮略圖。但在我的自定義適配器上沒有使用AsyncTask。我知道這是問題所在。 但是我怎樣才能管理我的自定義適配器使用AsyncTask?
http://stackoverflow.com/a/7730547/1187845 答案在這個環節是非常有益的。 –