1

so yeah i made this app and im using custom registration instead of the firebase one, but i also need this code to work for other functions of my porject. i need the code to stop inserting the values once it finds they already exist on my firebase realtime database, my code checks that they exist but then continues on with adding them to the database.

        private void checkDuplicates() {

            final String username = UserName.getText().toString();
            final String email = Email.getText().toString();
            final String phone = MobileNumber.getText().toString();
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.child("userName").equals(username)) {
                        Toast.makeText(Registeration.this, "Entered username is unique ", Toast.LENGTH_SHORT).show();
                            checkDup = false;
                    } else {
                        Toast.makeText(Registeration.this, "Entered username is  already registered", Toast.LENGTH_SHORT).show();
                        checkDup = true;

                        finish();

                    }


                    if (!dataSnapshot.child("email").equals(email)) {
                        Toast.makeText(Registeration.this, "Entered email is unique ", Toast.LENGTH_SHORT).show();
                        checkDup = false;

                    } else {
                        Toast.makeText(Registeration.this, "Entered email is  already registered", Toast.LENGTH_SHORT).show();
                        checkDup = true;

                        finish();

                    }


                    if (!dataSnapshot.child("mobileNumber").equals(phone)) {
                        Toast.makeText(Registeration.this, "Entered phone is unique ", Toast.LENGTH_SHORT).show();
                        checkDup = false;

                    } else {
                        Toast.makeText(Registeration.this, "Entered phone is  already registered", Toast.LENGTH_SHORT).show();
                        checkDup = true;

                        finish();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }

        private void CreateUserProfile() {


            checkDuplicates();

            if (checkDup == false) {

                user.setName(Name.getText().toString());
                user.setEmail(Email.getText().toString());
                user.setUserName(UserName.getText().toString());
                user.setMobileNumber(MobileNumber.getText().toString());
                user.setPassword(Password.getText().toString());
                user.setUser_Type(User_Type.getSelectedItem().toString());

                ref.child(user.getUserName()).setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(Registeration.this,
                                    "Registration Successful", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(Registeration.this,
                                    "Registration Failed", Toast.LENGTH_LONG).show();
                        }
                    }
                });
                progressdialog.setMessage("Registering User...");
                progressdialog.show();
                startActivity(new Intent(Registeration.this, Staff_Home.class));
                finish();

            }

            else {
                finish();
            }
        }

these methods are what i used, i get the alerts that either one of the values is already registered but then it goes into inserting them and moves back to another activity. i want it to show the alerts and allow me to change them instead of doing it all over again.

Roy
  • 11
  • 1
  • Data is loaded from Firebase asynchronously. Right now your `if (checkDup == false) {` runs before the data has been loaded from Firebase. The code that uses the `checkDup` result, should probably be inside your `finish` method. – Frank van Puffelen Apr 13 '19 at 14:04
  • @FrankvanPuffelen can you please explain more about where should the code that use `checkDup` should be used? i have used `finish()` multiple times and i dont know which one u meant – Roy Apr 13 '19 at 18:31
  • See https://stackoverflow.com/questions/52981256/check-if-an-email-exists-before-creating-a-user-firebase-android/52982269#52982269, where I pass the flag indicating the user already exists into a callback function. – Frank van Puffelen Apr 13 '19 at 23:47

0 Answers0