【问题标题】:How to add Progress Dialog of adding data to firebase in Android?如何在Android中添加向firebase添加数据的进度对话框?
【发布时间】:2019-01-30 12:45:36
【问题描述】:

进度对话框显示用户注册成功或失败的位置。有没有其他更好更有效的方法。提前致谢。

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 跨度不能有零长度 SPAN_EXCLUSIVE_EXCLUSIVE 跨度不能有零长度 08-24 13:19:00.354 6915-6915/com.addaj.mobilerecommendationsystem E/AndroidRuntime: FATAL EXCEPTION: main 进程: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(未知来源) 在 com.google.firebase.database.DatabaseReference.setValue(未知来源) 在 com.addaj.mobilerecommendationsystem.RegistrationActivity.SaveAccountInformation(RegistrationActivity.java:171) 在 com.addaj.mobilerecommendationsystem.RegistrationActivity.access$000(RegistrationActivity.java:29) 在 com.addaj.mobilerecommendationsystem.RegistrationActivity$1.onClick(RegistrationActivity.java:82) 在 android.view.View.performClick(View.java:5647) 在 android.view.View$PerformClick.run(View.java:22465) 在 android.os.Handler.handleCallback(Handler.java:754) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:163) 在 android.app.ActivityThread.main(ActivityThread.java:6228) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

【问题讨论】:

    标签: java android progressdialog


    【解决方案1】:

    解决方案:

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

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

    希望对你有帮助

    【讨论】:

    • :) ;) 非常欢迎
    • 让我们环聊吧!大声笑:P
    • 真的..? :D 你一定是在开玩笑
    猜你喜欢
    • 2013-07-07
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多