3

私はうまくいくと思っていた方法でこれを行うことにあまり成功していないので、専門家に尋ねます.

画像にリンクする 10 個の URL の ArrayList があります。最初の URL を 2 秒間表示してから、2 番目の URL を取得し、最後まで同じことを行います。

これが私がこれまでに持っているものです。おそらく、postExecute のダイアログで最善の方法で行っていないと思いますか?:

 private class LiveView extends AsyncTask<String, Integer, ArrayList<String>> {
        ProgressDialog dialog;
        private volatile boolean running = true;

        @Override
        protected void onPreExecute() {
             dialog = ProgressDialog.show(
                        myView.this,
                        "Working",
                        "Info message . . .",
                        true,
                        true,
                        new DialogInterface.OnCancelListener(){

                            public void onCancel(DialogInterface dialog) {
                                cancel(true);                           
                            }
                        }
                );
            }

        @Override
        protected void onCancelled() {
              running = false;
        }

        @Override
        protected ArrayList<String> doInBackground(String... passed) {



            while (running) {

            //removed the code here that sends the request to to make this shorter the server but it works fine
            return responseFromServer.arrayListofURLs;           //list or URLs 

            }
            return null;
        }

        @Override
        protected void onPostExecute(ArrayList<String> listURLs) {      
            dialog.cancel();

            Dialog liveView = new Dialog(myView.this, R.style.Dialog);
            liveView.setContentView(R.layout.liveview_dialogue);
            TextView title = (TextView)liveView.findViewById(R.id.liveViewTitle);           
            Button button = (Button) liveView.findViewById(R.id.liveViewButton);
            ImageView trackImage = (ImageView)liveView.findViewById(R.id.liveViewImage);

            //I want to loop through the ten images here? 

            button.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {

                }
            });
            liveView.show();



        }

    }
4

2 に答える 2

1

ハンドラーを作成し、postDelayed を介して 2 秒の遅延でメッセージを送信します。メッセージを受け取るたびに、trackImage.setImage を呼び出して次の画像を表示します。最終的にダイアログを閉じたら、保留中のメッセージをハンドラーから削除します。

于 2013-03-18T19:40:22.080 に答える
0

いくつかのコードで答えを終えるために、これが私がハンドラーでそれをした方法です。AsyncTask用にビットマップのListArrayをグローバルに作成したので、変数を渡す必要はありませんでした。また、ダイアログが閉じられた場合にハンドラーを終了するためにブール値を使用しました。

        liveView = new Dialog(myView.this, R.style.Dialog);
        liveView.setContentView(R.layout.liveview_dialogue);
        TextView title = (TextView)liveView.findViewById(R.id.liveViewTitle);           
        Button button = (Button) liveView.findViewById(R.id.liveViewButton);
        trackImage = (ImageView)liveView.findViewById(R.id.liveViewImage);





        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                run = false;
                liveView.dismiss();
            }
        });
        liveView.show();

        final Handler handler = new Handler(); 
        final Runnable r = new Runnable()
        {
            Iterator<Bitmap> it = images.iterator();
            public void run() 
            {   
                if(run){
                trackImage.setImageBitmap(it.next());
                if(it.hasNext())
                handler.postDelayed(this, 5000);
                }
            }
        };
        handler.postDelayed(r, 5000);
于 2013-03-19T06:21:31.963 に答える