2016-07-04 4 views
0

私はアンドロイドに新しいです、スレッドを管理する経験はあまりありません。私は、例えば5秒間進捗バーを表示し、その後繰り返すアクティビティに取り組んでいます。 5秒間、ユーザーがテキストを操作するためのテキストが表示されます。私はこれをN回繰り返す。Androidでの一連のタスクのProgressBarアクティビティの繰り返し

現在のところ、このような進捗状況の1つでは次のコードがあります。私はそれをループしようとしましたが、スレッドが同時に実行されるので、それは役に立ちませんでした。これを何回N回繰り返すことができますか?問題を解決するために私は正しい道にいるのですか?

public class test extends Activity { 

    private ProgressBar progressBar; 
    private int progressStatus = 0; 
    private TextView textView; 
    private Handler handler = new Handler(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_loop); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar1); 
     textView = (TextView) findViewById(R.id.textView1); 
     progressBar.setScaleY(3f); 
     // Start long running operation in a background thread 

     for(int i=0; i<5; i++) 
      Progress(); 

    } 

    public void Progress(){ 
     new Thread(new Runnable() { 
      public void run() { 
       while (progressStatus < 100) { 
        progressStatus += 1; 
        // Update the progress bar and display the 
        //current value in the text view 
        handler.post(new Runnable() { 
         public void run() { 
          progressBar.setProgress(progressStatus); 
          textView.setText(progressStatus+"/"+progressBar.getMax()); 
         } 
        }); 
        try { 
         // Sleep for 200 milliseconds. 
         //Just to display the progress slowly 
         Thread.sleep(100); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }).start(); 
    } 
} 
+0

'5秒間に、ユーザーがテキストを操作するためのテキストを表示します。 TextViewに表示されるテキストを操作するには?ユーザーは何をすべきですか? – greenapps

+0

一度に5つのスレッドを開始しないでください。スレッドが完了すると、次のスレッドを開始します。または、run()のコードをループ内で5回実行させます。 – greenapps

+0

ユーザーは5回進捗状況を見ているとはどういう意味ですか? – greenapps

答えて

0

私は私はあなたが以下の通りのパターンを使用することをお勧めしますが、ここのスレッドが同時にではなく、すべてのよりも、次々に実行するために取得する方法であるだろうかわからない:

public void progress(final int numberOfRuns) { 
    if (numberOfRuns <= 0) { 
     return; 
    } 
    new Thread(new Runnable() { 
     public void run() { 
      while (progressStatus < 100) { 
       progressStatus += 1; 
       // Update the progress bar and display the 
       //current value in the text view 
       handler.post(new Runnable() { 
        public void run() { 
         progressBar.setProgress(progressStatus); 
         textView.setText(progressStatus + "/" + progressBar.getMax()); 
         // For the UI Changes. Eg update the loop number 
         myTextView.setText(Integer.toString(totalLoop)); 
        } 
       }); 
       try { 
        // Sleep for 200 milliseconds. 
        //Just to display the progress slowly 
        Thread.sleep(100); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
      progressStatus = 0; 
      totalLoop = totalLoop+1; 
      progress(numberOfRuns - 1); 
     } 
    }).start(); 
} 

その後、ちょうどprogress(numberOfRuns)を呼び出し、ループの必要はありません。

+0

変数 'progressStatusが内部クラスからアクセスされ、最終宣言する必要があります'を取得しています。最後に、 'progressStatus + = 1;で行を変更することができます。 – impossible

+0

すっごくうまくいく。関数内で 'progressStatus'を再宣言しました。-_-コードを修正しています。ただメモしてください。 'progress()'を実行するたびにprogressStatusの値をリセットするので、whileループは毎回0から100まで実行されます。 –

+0

最後に私はそれをすることができました。私はあなたの答えをUIスレッドで更新しました。私は答えを受け入れることができるように編集を受け入れます。ありがとう:) – impossible

関連する問題