1

Android アプリケーションのプログレス バーを作成するための支援を受けています。ここにたくさんの助けがあります!私は修正するのに苦労しているという問題を抱えています。アプリケーションがネットワーク接続されたコンピューターからファイルをダウンロードしようとしている間、進行状況バーが表示されます。これは問題なく動作しますが、エラーが発生した場合に備えて UI を更新する必要があります。スレッド内で UI を更新できず、getRaceResultsHandler から UI を更新したい。残念ながら、スレッドが完了する前にそのコードを実行します。私は運がないことをいくつか試しました。誰かが助けてくれるなら、以下にコメント付きのコードサンプルがあります。

public void getRaceResultsHandler (View view) {

       dialog = new ProgressDialog(this);
       dialog.setCancelable(true);
       dialog.setMessage("Attempting to transfer race files. Please wait...");
       // Set progress style to spinner
       dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
       // display the progressbar
       dialog.show();

       // create a thread for downloading the files

       Thread background = new Thread (new Runnable() {
           public void run() {

                   //The Code here to execute the file download from the networked computer....

                  //Dismiss the progress bar because the download is either completed or failed...
                  dialog.dismiss();                         
           }        
        });
        // start the background thread
        background.start();


              //All Other Code Goes here to update the UI. Shows either an error message or a success based on the results of the download.
//My problem is that this code executes before the background thread is completed. I need it to wait until the thread is completed. 

}
4

1 に答える 1

0

を使用してそのダイアログを閉じてみてくださいHandler

Handler h = new Handler(); // Create this object in UI Thread



Thread background = new Thread (new Runnable() {
   public void run() {
     h.post(new Runnable()
     {

       public void run()
       {
         dialog.dismiss();
       }

     };
   });

AsyncTask通常の代わりに使用する必要がありますThread

AsyncTask<Void,Void,Void> aTask = new AsyncTask<Void,Void,Void>()
{

  @Override
  public void onPreExecute()
  {
      // Setup some UI Objects
  }
  @Override
  public void onPostExecute(Void result)
  {
     dialog.dismiss();

  }

  @Override
  protected Void doInBackground(Void...params)
  {
     // your download stuff
     publishProgress(object) // <-- if you want to update the progress of your download task
  }


});

注: 自分では試していません。友人のラップトップには IDE がありません。

于 2013-01-26T02:19:44.397 に答える