0

我正在使用此自定义适配器来显示 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?

4

1 回答 1

3

您收到错误是因为您在 UI 线程中进行网络访问。这被认为是不好的,因为网络访问需要很长时间,因此会阻塞 UI。

为了解决这个问题,您可以编写一个AsyncTask从网络中提取数据的doInBackground方法,然后onPostExecute用位图/可绘制对象填充视图。我认为 Android 文档有一个适用于这种情况的示例,但我现在找不到。

您还可以从我的 Zwitscher 应用程序中查看此源代码片段DownloadUserImageTaskurl 是从传递的 User 对象中检索的。然后将图片添加到视图中

userPictureView.setImageBitmap(result);   
于 2012-09-27T07:35:55.047 回答