没有完成其他部分回到if语句? (android和firebase)

时间:2017-12-12 07:03:37

标签: android firebase firebase-realtime-database

我有以下方法用于注册方案,创建之后会干杯帐户创建成功但之后再次显示部分吐司是否也像电话号码已存在

private void CreateAccount(String name, String phone, String password) {

        if(TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(password)) {
            Toast.makeText(SignUpActivity.this, "All fields required", Toast.LENGTH_LONG).show();
        } else {

            FirebaseDatabase database = FirebaseDatabase.getInstance();
            final DatabaseReference table_user = database.getReference("User");

            final ProgressDialog dialog = new ProgressDialog(SignUpActivity.this);
            dialog.setMessage("Please wait...");
            dialog.show();

            table_user.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    if(dataSnapshot.child(edtPhone.getText().toString()).exists()) {
                        dialog.dismiss();
                        Toast.makeText(SignUpActivity.this, "Phone Number Already Exists", Toast.LENGTH_SHORT).show();
                    } else {
                        dialog.dismiss();
                        User user = new User(edtName.getText().toString(), edtPassword.getText().toString());
                        table_user.child(edtPhone.getText().toString()).setValue(user);
                        Toast.makeText(SignUpActivity.this, "Account Created Successfully", Toast.LENGTH_SHORT).show();

                        Intent homeIntent = new Intent(SignUpActivity.this, HomeActivity.class);
                        homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(homeIntent);
                        finish();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    }
  

我做错了什么可以有人告诉我发生了什么以及如何解决   此

1 个答案:

答案 0 :(得分:1)

你有没有试过这个?

table_user.child(edtPhone.getText().toString()).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if(dataSnapshot.getValue() != null) {
                    dialog.dismiss();
                    Toast.makeText(SignUpActivity.this, "Phone Number Already Exists", Toast.LENGTH_SHORT).show();
                } else {
                    dialog.dismiss();
                    User user = new User(edtName.getText().toString(), edtPassword.getText().toString());
                    table_user.child(edtPhone.getText().toString()).setValue(user);
                    Toast.makeText(SignUpActivity.this, "Account Created Successfully", Toast.LENGTH_SHORT).show();

                    Intent homeIntent = new Intent(SignUpActivity.this, HomeActivity.class);
                    homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(homeIntent);
                    finish();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }