2012-09-27 57 views
0

©我正在使用此自定義適配器來顯示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?

+0

http://stackoverflow.com/a/7730547/1187845 答案在這個環節是非常有益的。 –

回答

3

由於您在UI線程中進行網絡訪問,您正在收到錯誤。這被認爲是不好的,因爲網絡訪問需要很長時間,從而阻止用戶界面。

爲了解決這種情況,可以寫一個AsyncTask拉動其doInBackground方法中從網的數據,然後在onPostExecute填充與位圖/可繪的圖。 我認爲Android文檔有一個這種情況的例子,但我現在找不到它。

您也可以從我的Zwitscher應用程序中查看此source code snippetDownloadUserImageTask;該url將從傳遞的User對象中檢索。然後將圖片添加到視圖中

userPictureView.setImageBitmap(result); 
相關問題