2013-08-28 55 views
0

嗨我有一些與Android的AsyncTask類的麻煩。這是我在Parse上登錄用戶的代碼。有不同的情況下,如密碼不正確等。異步任務,關閉aletdialog後進度對話框不會消失

我的問題是,錯誤檢查的作品和不同的情況下工作正常,但progressDialog按鈕不會消失後,我關閉alertdialog ...任何人都可以幫助我和這個?

這裏是我的代碼

private class LoadTask extends AsyncTask<Map<String, String>, Integer, Void> { 


     // called before running code in a separate thread 

     private Result resultCode; 
     private boolean isSuccess; 
     private ProgressDialog progressDialog; 


     public LoadTask(Activity activity) { 
      onPreExecute(); 
     } 
     @Override 
     protected void onPreExecute() { 
      progressDialog = ProgressDialog.show(TitlePage.this, 
      getString(R.string.login_progress_title), 
      getString(R.string.login_progress_message), false, false); 
     } 

     @Override 
     protected Void doInBackground(Map<String, String>... arg0) { 
      // Try to login with the given inputs 
      ParseUser user = null; 
      Map<String, String> argMap = arg0[0]; 
      try { 
       user = ParseUser.logIn(argMap.get("username"), argMap.get("password")); 
      } catch (ParseException e) { 
       e.fillInStackTrace(); 
       boolean errorOccured = false; 
       List<ParseObject> usernameResults = new ArrayList<ParseObject>(); 
       List<ParseObject> passwordResults = new ArrayList<ParseObject>(); 
       ParseQuery query = ParseUser.getQuery(); 
       // try to find the username that the user typed in 
       query.whereEqualTo("username", argMap.get("username")); 
       try { 
        query.count(); 
        usernameResults = query.find(); 
       } catch (ParseException e1) { 
        // error occured trying to find the username 
        errorOccured = true; 
        e1.printStackTrace(); 
       } catch (NullPointerException e1) { 
        errorOccured = true; 
        e1.printStackTrace(); 
       } 

       // try to find the password that the user typed in 
       // associated with that username 
       query.whereEqualTo("username", argMap.get("username")); 
       query.whereEqualTo("password", argMap.get("password")); 
       try { 
        query.count(); 
        passwordResults = query.find(); 
       } catch (ParseException e1) { 
        // error occured trying to find the password 
        errorOccured = true; 
        e1.printStackTrace(); 
       } catch (NullPointerException e1) { 
        errorOccured = true; 
        e1.printStackTrace(); 
       } 

       // figure out the error 
       if (errorOccured) { 
        resultCode = Result.UNEXPECTED_ERROR; 
       // buildAlertDialog(R.string.error_login_title, R.string.error_login_unexp); 
       } 
       if ((usernameResults.size() == 0) && (passwordResults.size() == 0)) { 
        resultCode = Result.BOTH_INCORRECT; 
       // buildAlertDialog(R.string.error_login_title, R.string.error_login_combo); 
       } else if ((usernameResults.size() == 0) && (passwordResults.size() != 0)) { 
        resultCode = Result.USERNAME_INCORRECT; 
        //buildAlertDialog(R.string.error_login_title, R.string.error_login_uname); 
       } else if ((usernameResults.size() != 0) && (passwordResults.size() == 0)) { 
        resultCode = Result.PASSWORD_INCORRECT; 
        //buildAlertDialog(R.string.error_login_title, R.string.error_login_pswd); 
       } else { 
        // unexpected error 
        resultCode = Result.UNEXPECTED_ERROR; 
       // buildAlertDialog(R.string.error_login_title, R.string.error_login_unexp); 
       } 
       isSuccess = false; 
       return null; 
      } 
      // Check for verified email 
      boolean emailVerified = user.getBoolean("emailVerified"); 
      if (!emailVerified) { 
       resultCode = Result.EMAIL_NOT_VERIFIED; 
       ParseUser.logOut(); 
      } 
      isSuccess = true; 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      progressDialog.dismiss(); 
      if (isSuccess) { 
       TitlePage.this.setReturnStatus(isSuccess); 
      } else { 
       System.out.println("THIS IS RESULT CODE " + resultCode); 
       if (resultCode == Result.UNEXPECTED_ERROR) { 
        buildAlertDialog(R.string.error_login_title, R.string.error_login_unexp); 
       } else if (resultCode == Result.BOTH_INCORRECT) { 
        buildAlertDialog(R.string.error_login_title, R.string.error_login_combo); 
       } else if (resultCode == Result.USERNAME_INCORRECT) { 
        buildAlertDialog(R.string.error_login_title, R.string.error_login_uname); 
       } else if (resultCode == Result.PASSWORD_INCORRECT) { 
        buildAlertDialog(R.string.error_login_title, R.string.error_login_pswd); 
       } else { 
        buildAlertDialog(R.string.error_login_title, R.string.error_login_verif); 
       } 
       TitlePage.this.setReturnStatus(isSuccess); 
      } 
     } 
    } 

這裏是我的代碼在我的主要活動運行的異步任務

Map<String, String> argMap = new HashMap<String, String>(); 
argMap.put("username", usernameString); 
argMap.put("password", passwordString); 
LoadTask task = new LoadTask(this); 
task.execute(argMap); 


private void buildAlertDialog(final int title, final int message) { 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
    // set title 
    alertDialogBuilder.setTitle(title); 
    // set dialog message 
    alertDialogBuilder 
     .setMessage(message) 
     .setCancelable(false) 
     .setPositiveButton(R.string.close_alert, new DialogInterface.OnClickListener() { 
     public void onClick(final DialogInterface dialog, final int id) { 
      // if this button is clicked, close the dialog box 
      dialog.cancel(); 
     } 
    }); 

    // create alert dialog 
    AlertDialog alertDialog = alertDialogBuilder.create(); 

    // show the message 
    alertDialog.show(); 
    if (progressDialog.isShowing()) { 
     progressDialog.dismiss(); 
    } 
} 

謝謝

+0

發佈您的buildAlertDialog。你從那裏解僱你的進展對話嗎? –

+0

嘗試添加progressDialog = new ProgressDialog(context);在您撥打progressDialog.show(...)方法之前 – techieWings

+0

謝謝您的幫助! – James

回答

2

,我們就能取得進展對話框,在同一以下示例:

@Override 
protected void onPreExecute() { 
     loadingDailog = new ProgressDialog(context,AlertDialog.THEME_HOLO_LIGHT); 
    ((ProgressDialog) loadingDailog).setIndeterminate(true); 
    ((ProgressDialog) loadingDailog).setProgressStyle(ProgressDialog.THEME_HOLO_LIGHT); 


    loadingDailog.setMessage("Loading..."); 
    loadingDailog.show(); 
    } 

當你想消除這個吧:

if (loadingDailog != null && loadingDailog.isShowing()) { 
     loadingDailog.dismiss(); 
    } 
+0

謝謝你的幫助! – James

+0

感謝您的幫助:) – Mutawe