2011-11-28 18 views
4

我正在使用AsyncTask來加載Rss新聞時顯示progressBar。因爲這是使用的AsyncTask我不知道如果我的方法是正確的我第一次IM,所以你可以告訴我,如果它看起來不錯,你呢?它的工作,但我想只是爲了粟特!謝謝如何使用AsyncTask來顯示ProgressBar並加載Rss

public class BackgroundAsyncTask_nea extends 
     AsyncTask<Void, Void, Void> { 
      private ProgressDialog dialog; 
     int myProgress; 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      displayRss(); 
      dialog.dismiss(); 


     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
       dialog = ProgressDialog.show(nea.this, "", "Loading. Please wait...", true); 
      myProgress = 0; 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      // TODO Auto-generated method stub 
      //super.onProgressUpdate(values); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      // TODO Auto-generated method stub 
      loadFeed(); 
      return null; 
     } 



    } 

loadFeed();

private void loadFeed(){ 
     try{ 
      BaseFeedParser parser = new BaseFeedParser(); 
      messages = parser.parse(); 

      } 
     catch (Throwable t){ 
      Log.e("OSFP.News",t.getMessage(),t); 


      finish(); 
     } 
    } 

displayRss();

private void displayRss(){ 

     ArrayList<HashMap<String, String>> List_nea = new ArrayList<HashMap<String, String>>(messages.size()); 


     for (Message msg : messages){ 

      des.add(msg.getDescription());// keimeno 
      text.add(msg.getTitle());// titlos 
      url.add(msg.getLink());// link 
      imgl.add(msg.getImgLink()); 

     HashMap<String, String> map = new HashMap<String, String>(); 
     map.put("name", msg.getTitle()); 
     map.put("date", msg.getDate());  


     List_nea.add(map); 
     ListAdapter mSchedule = new SimpleAdapter(this, List_nea, R.layout.row, 
       new String[] {"name", "date"}, new int[] {R.id.TextView01, R.id.TextView02}); 
     this.setListAdapter(mSchedule); 
     } 

    AnimationSet set = new AnimationSet(true); 

    Animation animation = new AlphaAnimation(0.0f, 1.0f); 
    animation.setDuration(400); 
    set.addAnimation(animation); 

    animation = new TranslateAnimation(
     Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f, 
     Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f 
    ); 

    animation.setDuration(400); 
    set.addAnimation(animation); 
    LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f); 
    ListView listViewn = getListView();   
    listViewn.setLayoutAnimation(controller); 

    } 
+0

[Progressbar togther with asyncTask]的可能重複(http://stackoverflow.com/questions/4119009/progressbar-togther-with-asynctask) – Sameer

回答

2

爲了更新您所需要的進度條將使得該publishProgress()函數調用。你在代碼中沒有做到這一點。如果我是你,我會將AsyncTask的引用傳遞給你的loadFeed函數。類似這樣的:

protected Void doInBackground(Void... arg0) { 
     // TODO Auto-generated method stub 
     loadFeed(this); 
     return null; 
    } 

private void loadFeed(AsyncTask<Void, Void, Void> asyncTask){ 
     // make calls to publishProgress in here. 
      try{ 
      BaseFeedParser parser = new BaseFeedParser(); 
      messages = parser.parse(); 

      } 
     catch (Throwable t){ 
      Log.e("OSFP.News",t.getMessage(),t); 


      finish(); 
     } 
    } 

您還需要實施onProgressUpdate()方法,但您還沒有。這是您實際更新進度欄的值的位置。

 protected void onProgressUpdate(Integer... progress) { 
     dialog.setProgress(progress[0]); 
    } 
+0

更新了我的答案。至於沒有互聯網連接和在加載時使用敬酒的應用程序崩潰,這些是不同的問題。爲他們提出新的問題。但是,在你做這件事之前,先試着做一些研究(例如谷歌)。 –

+0

我真的非常感謝您的幫助!:) –

0

是的。 這是正確的工作流程,顯示在UI線程上執行之前的進度對話框(也可以在此類之外完成,但這種方式似乎更好),在doInBackground方法上進行工作,使用publishProgress()更新ProgressDialog if需要的話,然後關閉ProgressDialog並在UI線程中顯示onPostExecute()的結果。

0

在loadFeed解析器中,您可以使用AsyncTask的參考和方法publishProgress將進度更新發送到onProgressUpdate,您可以在其中更新進度欄。

相關問題