2011-11-28 55 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,您可以在其中更新进度栏。

相关问题