Daisy
Daisy

Reputation: 527

How to add Progress Dialog of adding data to firebase in Android?

Where to put the progress dialog show whether the user successful or unsuccessful to register their-self. Is there any other ways which is more better and more effective. Thanks in advance.

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 as shown below:-

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$3 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$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)

Upvotes: 2

Views: 453

Answers (1)

Solution:

Add this method and your project should work fine:

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

            }
        }
    });

}

Hope it helps

Upvotes: 1

Related Questions