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()エラーが発生します。

Webから行の画像を表示し、このエラーが発生しないようにするにはどうすればよいですか?

AsyncTaskのlistviewメインデータを使用しています。このデータリストでは、各アイテムに一意のYouTube動画IDがあります。このIDを使用して、Webからビデオサムネイルを取得しています。しかし、私のカスタムアダプタではAsyncTaskを使用しませんでした。私はそれが問題であることを知っています。しかし、AsyncTaskを使用するためにカスタムアダプタを管理するにはどうすればよいですか?

4

1 に答える 1

3

UIスレッドでネットワークアクセスを行うため、エラーが発生します。ネットワークアクセスに時間がかかり、UIがブロックされるため、これは悪いと見なされます。

To solve this, you can write an AsyncTask that pulls the data from the net within its doInBackground method and then in onPostExecute fills the view with the bitmap/drawable. I think the Android documentation has an example for this scenario, but I can't find it right now.

You can also have a look at this source code snippet, DownloadUserImageTask from my Zwitscher app; the url is retrieved from the passed User object. The picture is then added to the view in

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