如何在安卓中添加向Firebase添加数据的进度对话框?



在哪里放置进度对话框显示用户注册自己的成功与否。有没有其他方法更好,更有效。提前谢谢。

RegisterButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
SaveAccountInformation();
}
});

private void SaveAccountInformation()
{
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
{
loadingBar.setTitle("Registration Account");
loadingBar.setMessage("Please wait while we are registering you in our system.");
loadingBar.show();
loadingBar.setCanceledOnTouchOutside(true);
String userid = databaseUser.push().getKey();
User user = new User(userid, firstname, lastname);
databaseUser.child(userid).setValue(user);
Toast.makeText(this, "Registration added!", Toast.LENGTH_SHORT).show();
databaseUser.child(userid).setValue(user,new OnCompleteListener()
{
@Override
public void onComplete(@NonNull Task task)
{
if(task.isSuccessful())
{
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();
}
else
{
String message = task.getException().getMessage();
Toast.makeText(RegistrationActivity.this, "Error occured: " +message, Toast.LENGTH_SHORT).show();
}
loadingBar.dismiss();
}
});
}
}

日志猫如下所示:-

08-24 13:18:50.865 6915-6915/com.addaj.mobilerecommendationsystem E/SpannableStringBuilder:SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有零长度 SPAN_EXCLUSIVE_EXCLUSIVE跨度不能具有零长度 08-24 13:19:00.354 6915-6915/com.addaj.mobilerecommendationsystem E/Android运行时:致命 异常:主要 进程: com.addaj.mobilerecommendationsystem, PID: 6915 com.google.firebase.database.DatabaseException: 无法解析具有类类 com.addaj.mobilerecommendationsystem.RegistrationActivity$3 的节点 在 com.google.android.gms.internal.firebase_database.zzjd.zza(来源不明( 在 com.google.android.gms.internal.firebase_database.zzjg.zzc(未知来源( at com.google.firebase.database.DatabaseReference.setValue(Unknown Source( at com.addaj.mobilerecommendationsystem.RegistrationActivity.SaveAccountInformation(RegistrationActivity.java:171( at com.addaj.mobilerecommendationsystem.RegistrationActivity.access$000(RegistrationActivity.java:29( at com.addaj.mobilerecommendationsystem.RegistrationActivity$1.onClick(RegistrationActivity.java:82( at android.view.View.performClick(View.java:5647( at android.view.View$PerformClick.run(View.java:22465( at android.os.Handler.handleCallback(Handler.java:754( at android.os.Handler.dispatchMessage(Handler.java:95( at android.os.Looper.loop(Looper.java:163( at android.app.ActivityThread.main(ActivityThread.java:6228( at java.lang.reflect.Method.invoke(Native Method( at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904( at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794(

解决方案:

添加此方法,您的项目应该可以正常工作:

private void uploadImageAndGetURL(String ImageId) {
final StorageReference filePath = storageImage.child(ImageId + ".jpg");
UploadTask uploadTask = filePath.putFile(imageUri);
uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
downloadURL = task.getResult().toString();
storeDataToFirebase();
} else {
Toast.makeText(AddAdsActivity.this, "There has bean a problem in the database.", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}

希望对你有帮助

最新更新