如何在Android中添加向firebase添加数据的进度对话框?
How to add Progress Dialog of adding data to firebase in Android?
进度对话框放在哪里显示用户注册成功或失败。有没有其他更好更有效的方法。提前致谢。
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();
}
});
}
}
Logcat如下图:-
08-24 13:18:50.865 6915-6915/com.addaj.mobilerecommendationsystem E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-24 13:19:00.354 6915-6915/com.addaj.mobilerecommendationsystem E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.addaj.mobilerecommendationsystem, PID: 6915
com.google.firebase.database.DatabaseException: Failed to parse node with class class com.addaj.mobilerecommendationsystem.RegistrationActivity
at com.google.android.gms.internal.firebase_database.zzjd.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzjg.zzc(Unknown Source)
at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
at com.addaj.mobilerecommendationsystem.RegistrationActivity.SaveAccountInformation(RegistrationActivity.java:171)
at com.addaj.mobilerecommendationsystem.RegistrationActivity.access[=12=]0(RegistrationActivity.java:29)
at com.addaj.mobilerecommendationsystem.RegistrationActivity.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();
}
}
});
}
希望对您有所帮助
进度对话框放在哪里显示用户注册成功或失败。有没有其他更好更有效的方法。提前致谢。
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();
}
});
}
}
Logcat如下图:-
08-24 13:18:50.865 6915-6915/com.addaj.mobilerecommendationsystem E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 08-24 13:19:00.354 6915-6915/com.addaj.mobilerecommendationsystem E/AndroidRuntime: FATAL EXCEPTION: main Process: com.addaj.mobilerecommendationsystem, PID: 6915 com.google.firebase.database.DatabaseException: Failed to parse node with class class com.addaj.mobilerecommendationsystem.RegistrationActivity at com.google.android.gms.internal.firebase_database.zzjd.zza(Unknown Source) at com.google.android.gms.internal.firebase_database.zzjg.zzc(Unknown Source) at com.google.firebase.database.DatabaseReference.setValue(Unknown Source) at com.addaj.mobilerecommendationsystem.RegistrationActivity.SaveAccountInformation(RegistrationActivity.java:171) at com.addaj.mobilerecommendationsystem.RegistrationActivity.access[=12=]0(RegistrationActivity.java:29) at com.addaj.mobilerecommendationsystem.RegistrationActivity.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();
}
}
});
}
希望对您有所帮助