0

下面写的方法用来显示progressDialog(Just a spinner),

我已将 setCancelable 和 setIndeterminate 设置为 false,因为我不希望用户关闭此 progressDailog。我的问题是,我不知道如何在完成 AsyncTask 后关闭此 progressDialog。请帮助我,我在谷歌上搜索了几个小时来解决同样的问题,看到很多类似的问题,但这些都没有解决我的问题!

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();
}

}
4

3 回答 3

2

您将需要调用progressDialog.dismiss()onPostExecute关闭progressDialog:

   @Override
    protected void onPostExecute(String result) {

           if (progressDialog !=null && progressDialog.isShowing())  
                progressDialog.dismiss(); //<<dismiss progressDialog here
            }
        }
于 2013-03-27T08:09:24.790 回答
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

于 2013-03-27T08:09:48.120 回答
0

Dismiss,方法中的对话框onPostExecute

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

               // Update your UI.
                 }
于 2013-03-27T08:09:51.887 回答