完成Async任务后如何关闭progressDialog

时间:2013-03-27 08:06:10

标签: android progressdialog

下面写的方法用于显示progressDialog(只是一个微调器),

我已将setCancelable和setIndeterminate设置为false,因为我不希望用户关闭此progressDailog。 我的问题是什么,我不知道如何关闭这个进程.Dialog upoun完成了AsyncTask.Please帮我,我谷歌搜索了几个小时来解决同样的问题,看到很多类似的问题,但没有一个解决了我的问题!!

public class ListFriends extends ListActivity { 
         int intStat; 
         private static final int HOME = 0;
            private static final int ADD = 1;
            private static final int LOGOUT = 2;
            private static final int SETTINGS = 3;
            //private String friend;
            private String user;
            private String deviceUser;
            private String friendNameGlobal;
                        ....
                        ....

 public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
                    ........
                    ........
                    loadFromServer(user);
}
private void loadFromServer(String userReceived) {
        //buildToast("Please Wait while Updating The List");
        showProgress();
        String URL = "http://"+server+"/friendlistmob.php?userid="+userReceived;
         new DownloadXmlTask().execute(URL);
        }
  private class DownloadXmlTask extends AsyncTask<String, Void, String> {
                    .....
                    .....
  }

  protected void onPostExecute(String result) {
                    progressDialog.dismiss();//not working
 }
                   ....
                    ....

public void showProgress()
{
ProgressDialog progressDialog = new ProgressDialog(this);   
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.setMessage("Please Wait while updating List");        
progressDialog.show();
}

}

3 个答案:

答案 0 :(得分:2)

您需要致电progressDialog.dismiss()中的onPostExecute来解除progressDialog:

   @Override
    protected void onPostExecute(String result) {

           if (progressDialog !=null && progressDialog.isShowing())  
                progressDialog.dismiss(); //<<dismiss progressDialog here
            }
        }

答案 1 :(得分:2)

public class ListFriends extends ListActivity { 
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    pd= new ProgressDialog(ListActivity.this);
    pd.setTitle("Displaying dialog");  
    new DownloadXmlTask().execute();  
  }



class DownloadXmlTask extends AsyncTask<Void,Void,Void>
 {
      protected void onPreExecute()
      {           super.onPreExecute();
                //display progressdialog.
                 pd.show();
      } 

       protected void doInBackground(Void ...params)
      {  
            //http request. do not update ui here

            return null;
      } 

      //can also show progressupdate

       protected void onPostExecute(Void result)
      {     
                super.onPostExecute(result);
                //dismiss progressdialog.
                //update ui
                pd.dismiss(); 
      } 

   }
}

http://developer.android.com/reference/android/os/AsyncTask.html

答案 2 :(得分:0)

关闭,onPostExecute方法中的对话框。

             @Override
    protected void onPostExecute(Void unused) {
               progressDialog.dismiss();

               // Update your UI.
                 }