2014-09-16 5 views
0

내 안드로이드 전화에서) 완료하려면 2-3 초 걸리는 내 클래스의 사용자 지정 메서드가 있고, 나는 그것을 진행률 표시 줄에 묶고 싶습니다.메서드의 안드로이드 진행률 막대

여기 내 방법 :

public void getQuestionsForSelectedCategory(){ 
    ArrayList<Question> temp = (ArrayList<Question>) this.clone(); 
    ArrayList<Question> tempGroup; 
    this.clear(); 
    for(int i=0;i<2;i++){ 
     tempGroup = new ArrayList<Question>(); 
     for(int j=0;j<temp.size();j++) 
      if((temp.get(j).getGroup()==i+1)&&(temp.get(j).getCategory().contains(category)||temp.get(j).getCategory().equals("*"))) 
       tempGroup.add(temp.get(j)); 
     getQuestionsForSelectedGroup(tempGroup, numbersByGroup[i], pointsByGroup[i]); 
    } 

    tempGroup = new ArrayList<Question>(); 
    for(int i=0;i<temp.size();i++){ 
     int a = temp.get(i).getGroup(); 
     if((a==3||a==4||a==5||a==6||a==7)) 
      if(temp.get(i).getCategory().contains(category)||temp.get(i).getCategory().equals("*")) 
       tempGroup.add(temp.get(i)); 
    } 
    Collections.shuffle(tempGroup); 
    getQuestionsForSelectedGroup(tempGroup, numbersByGroup[2], pointsByGroup[2]); 

    if(category.equals("C")){ 
     tempGroup = new ArrayList<Question>(); 
     for(int i=0;i<temp.size();i++) 
      if(temp.get(i).getCategory().equals(category)) 
       tempGroup.add(temp.get(i)); 
     getQuestionsForSelectedGroup(tempGroup, 10, 30); 
    } 
} 

그리고 여기에 내가 수행하려고 할 것입니다 : 진행 표시 줄이 100 채 웁니다 현재 코드에 대한

barProgressDialog = new ProgressDialog(this); 
    barProgressDialog.setTitle("Preparing Test"); 
    barProgressDialog.setMessage("Preparing Test"); 
    barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL); 
    barProgressDialog.setProgress(0); 
    barProgressDialog.setMax(100); 
    barProgressDialog.show(); 

    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       getQuestionsForSelectedCategory(); 
       while (barProgressDialog.getProgress() <= barProgressDialog.getMax()) { 
        updateBarHandler.post(new Runnable() { 
         public void run() { 
          barProgressDialog.incrementProgressBy(2); 
          } 
         }); 
        if (barProgressDialog.getProgress() == barProgressDialog.getMax()) { 
         barProgressDialog.dismiss(); 
        } 
       } 
      } catch (Exception e) { 
      } 
     } 
    }).start(); 
} 

을하지만 아무것도하지 않습니다.

+0

최소한의 방법으로 context.setProgressBarIndeterminate (true)를 사용할 수 있습니다. 장기 실행 메소드를 시작한 다음 끝에 false로 설정하십시오. github에는 진행 막대를 표시 할 라이브러리가 많이 있습니다. 그렇지 않으면 끝날 때까지 무한 반복되는 애니메이션 또는 클립 이미지를 볼 수 있습니다. –

답변

0

작업 중에 진행 상황을 게시하는 동안 AsyncTask를 사용하여이를 수행 할 수 있습니다.

AsyncTask<Void, Integer, Void> task = new AsyncTask<Void, Integer, Void>() { 

    @Override 
    protected Void doInBackground(Void... voids) { 
     getQuestionsForSelectedCategory(); 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     barProgressDialog.setProgress(values[0]); 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     barProgressDialog.dismiss(); 
    } 
} 

task.execute(); 

getQuestionsForSelectedCategorygetQuestionsForSelectedGroup 방법은 AsyncTask를 내부 및 진행 대화 상자를 업데이트 할 publishProgress(int progress)를 호출 할 수있는 루프 내에 있는지 확인합니다.