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

时间:2018-08-24 08:39:58

标签: java android firebase-realtime-database progressdialog

我的编码在将数据保存在实时数据库中的地方起作用。但是,进度对话框不起作用。如何编写编码以显示是否无法将数据保存在实时数据库中?

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

2 个答案:

答案 0 :(得分:2)

据我对您的问题的了解,您想跟踪数据是否正确保存,同时显示进度对话框。我想您正在使用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();
    }
}

希望有帮助。

答案 1 :(得分:0)

 private ProgressDialog  progressDialog = new ProgressDialog(Login.this);

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

 progressDialog.dismiss();//close progressDialog