Firebase实时数据库没有注册Android数据

时间:2018-02-28 12:59:39

标签: java android firebase firebase-realtime-database

我试图添加一个表" user"到我的Firebase实时数据库,代码一切看起来很好,但没有显示寄存器!

这是我的代码示例:

private void CreateUser(){
    final TextView firstName = findViewById(R.id.signUpOnSuccess_FirstName), lastName = findViewById(R.id.signUpOnSuccess_LastName);
    final EditText bday = findViewById(R.id.SignUpOnSuccess_Bday);
    final Spinner gender = findViewById(R.id.signUpOnSuccess_Gender);

    final String sFirstName = firstName.getText().toString().trim(),
                sLastName = lastName.getText().toString().trim(),
                sGender = gender.getSelectedItem().toString();

    //Transfer from SignUpActivity.java
    String sEmail = SignUpActivity.sEmail,
            sPassword = SignUpActivity.sPassword;

    if (!TextUtils.isEmpty(sFirstName)) {
        if (!TextUtils.isEmpty(sLastName)) {

            String id = databaseUsers.push().getKey();
            User user = new User(id, sEmail, sPassword, sFirstName, sLastName, sGender);

            databaseUsers.child(id).setValue(user);

            Toast.makeText(getApplicationContext(), "User Entry Successfull", Toast.LENGTH_SHORT).show();
        }else {Toast.makeText(getApplicationContext(), "Enter Last Name!", Toast.LENGTH_SHORT).show();}
    } else {Toast.makeText(getApplicationContext(), "Enter First Name Address!", Toast.LENGTH_SHORT).show();}
}

- " -

 String id = databaseUsers.push().getKey();
        User user = new User(id, sEmail, sPassword, sFirstName, sLastName, sGender);

        databaseUsers.child(id).setValue(user);

以下是我的Firebase规则: enter image description here

以下是Firebase数据库中显示的内容: enter image description here

2 个答案:

答案 0 :(得分:0)

尝试给它一些节点并将其保存在其中:

DatabaseReference databaseUsers = getDatabase().getReference("users");
String id = databaseUsers.push().getKey();
User user = new User(id, sEmail, sPassword, sFirstName, sLastName, sGender);
databaseUsers.child(id).setValue(user);

答案 1 :(得分:0)

在课程开头添加:

public FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
public DatabaseReference databaseUsers = databaseUsers.getReference("NameofYourTable");

你的其余代码似乎很好:

String id = databaseUsers.push().getKey();
User user = new User(id, sEmail, sPassword, sFirstName, sLastName, sGender);
databaseUsers.child(id).setValue(user);

要尝试查明操作是否存在问题,可以在将数据添加到firebase数据库时添加CompletionListener。

改变这个:

databaseUsers.child(id).setValue(user);

为此:

databaseUsers.child(id).setValue(userAux, new DatabaseReference.CompletionListener() {
        public void onComplete(DatabaseError error, DatabaseReference ref) {
            if(error == null){
                callback.OnSuccess("Ok");
            }
            else{
                callback.OnFailure(error.toString());
            }
        }
    });

只是检查操作是否正在进行(OnSuccess)或者是否(OnFailure),以及为什么不:

error.toString()