1

サーバーからの画像を処理してImageView
に表示するために、AsyncTaskクラスを使用しています。コードに[次へ]ボタンが1つあり、次のコードを使用して次の画像を呼び出します。

private class LoadImage extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPostExecute(Void result) {

        if (imgque != null) {
            imgSelectedQue.setImageDrawable(imgque);
            if (getActivity() != null) {
                adjustResourceEnvelope();
            }
        }

    }

    @Override
    protected void onPreExecute() {
        imgque = null;
        imgSelectedQue.setImageDrawable(null);
        imgSelectedQue.requestLayout();
    }

    @SuppressWarnings("deprecation")
    @Override
    protected Void doInBackground(Void... params) {
        InputStream is;
        try {
            is = (InputStream) new URL(Constants.PLAYER_SERVICE_BASE_URL + "/playerservice"
                    + imageurl).getContent();
            imgque = Drawable.createFromStream(is, null);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            imgque = null;
            e.printStackTrace();
        }

        return null;

    }
}

ここで問題となるのは、次のボタンを繰り返しクリックすると、 AsyncTaskが何度も呼び出され、すべてのURL画像がキューにあるため、ImageViewが「スライドショー」のようなすべての画像を表示することです。
最後のAsyncTask呼び出し画像のみを表示する方法はありますか?

4

2 に答える 2

1

このようなことをします:

[1][次へ]ボタンLoadImage AsyncTaskをクリックして呼び出す場合。NEXTdisable

[2]ボタンのonPreExecute()方法についてもう一度。LoadImage AsyncTask enableNEXT

また、

シンプルでもそういうことができますFLAG。trueとfalseに設定します。

これがお役に立てば幸いです。

ありがとう。

于 2012-12-15T10:19:46.783 に答える
0

私はこれに対する解決策を見つけました。
同じ問題に直面している人がいれば。
新しい行を追加しました

if (loadimage != null)
 {
   loadimage.cancel(true);
   loadimage = null;
}

loadimage.cancel(true) will stop the AsyncTask if it is already Running

次に、AsycTask()を実行します

loadimage = new LoadImage();
loadimage.execute();

このようにして、必要な最後のAsyncTaskのみを呼び出すことができます。

于 2012-12-15T11:09:34.287 に答える