My coding works where the data is saved in the real-time database. However, the progress dialogue does not work. How to put a coding to show if the data failed to be saved in the real-time database?
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();
}
As far as I have understood about your problem you want to track if the data is saved properly or not and show a progress dialog in the meantime. I suppose you are using Firebase database and saving the values under a specific node.
You might consider having a callback function here I suppose while saving the data in your Firebase real-time database.
I am rewriting the code. Please note that the code is not tested and modify if you get any syntax errors.
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();
}
}
Hope that helps.
private ProgressDialog progressDialog = new ProgressDialog(Login.this);
progressDialog.show();
progressDialog.setMessage(getResources().getString(R.string.loading));
progressDialog.setCancelable(false);
progressDialog.dismiss();//close progressDialog
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.