为什么进度对话框不显示?

Why the progress dialog does not show?

我的编码在数据保存在实时数据库中的地方起作用。但是,进度对话不起作用。实时数据库保存失败如何打码显示?

private void SaveAccountInformation() {

    loadingBar.setTitle("Registration Account");
    loadingBar.setMessage("Please wait while we are registering you in our system.");
    loadingBar.show();
    loadingBar.setCanceledOnTouchOutside(true);

    String firstname = RegisterFirstName.getText().toString();
    String lastname = RegisterLastName.getText().toString();

    if (TextUtils.isEmpty(firstname))
        Toast.makeText(RegistrationActivity.this, "Enter your first name.", Toast.LENGTH_SHORT).show();

    else if (TextUtils.isEmpty(lastname))
        Toast.makeText(RegistrationActivity.this, "Last name is required.", Toast.LENGTH_SHORT).show();

    else {
        String userid = databaseUser.push().getKey();
        User user = new User(firstname, lastname);
        databaseUser.child(userid).setValue(user);
        Toast.makeText(RegistrationActivity.this, "Registration has been successful.", Toast.LENGTH_SHORT).show();

        Intent HomeIntent = new Intent(RegistrationActivity.this, home.class);
        HomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(HomeIntent);
        finish();
    }

    loadingBar.dismiss();
}
 private ProgressDialog  progressDialog = new ProgressDialog(Login.this);

 progressDialog.show();
 progressDialog.setMessage(getResources().getString(R.string.loading));
 progressDialog.setCancelable(false);

 progressDialog.dismiss();//close progressDialog

据我了解您的问题,您想要跟踪数据是否正确保存并同时显示进度对话框。我想您正在使用 Firebase 数据库并将值保存在特定节点下。

我想您可能会考虑在此处设置回调函数,同时将数据保存在您的 Firebase 实时数据库中。

我正在重写代码。请注意,如果您遇到任何语法错误,代码未经测试和修改。

private void SaveAccountInformation() {

    ProgressDialog loadingBar = new ProgressDialog(RegistrationActivity.this);
    loadingBar.setMessage("Please wait while we are registering you in our system.");
    loadingBar.show();
    loadingBar.setCanceledOnTouchOutside(true);

    String firstname = RegisterFirstName.getText().toString();
    String lastname = RegisterLastName.getText().toString();

    if (TextUtils.isEmpty(firstname))
        Toast.makeText(RegistrationActivity.this, "Enter your first name.", Toast.LENGTH_SHORT).show();

    else if (TextUtils.isEmpty(lastname))
        Toast.makeText(RegistrationActivity.this, "Last name is required.", Toast.LENGTH_SHORT).show();

    else {

        String userid = databaseUser.push().getKey();
        User user = new User(firstname, lastname);

        databaseUser.child(userid).setValue(user)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Toast.makeText(RegistrationActivity.this, "Data saved successfully!", Toast.LENGTH_SHORT).show();
                    loadingBar.dismiss();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(RegistrationActivity.this, "Failed to save the data", Toast.LENGTH_SHORT).show();
                    loadingBar.dismiss();
                }
            }); 

        Intent HomeIntent = new Intent(RegistrationActivity.this, home.class);
        HomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(HomeIntent);
        finish();
    }
}

希望对您有所帮助。