简体   繁体   中英

Firebase realtime database not registering data Android

I'm trying to add a table "user" to my Firebase real-time database, code wise everything looks fine but there are no registers showing!

Here's my code sample:

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

Here are my Firebase rules: 在此处输入图片说明

Here's what's shown on Firebase Database: 在此处输入图片说明

Try Giving it some node and save it inside it like :

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

Add this at the beginning of the class:

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

The rest of your code seems fine:

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

To try to find out if there is a problem with the operation, you can add the CompletionListener, when you add the data into firebase database.

Change this:

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

For this:

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

Is just checking if the operation is going through(OnSuccess) or if is not (OnFailure), and why not:

error.toString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM