简体   繁体   中英

Firebase is not receiving data from Android Studio

Firebase database is not receiving data from Android. I'm trying every possible way for Firebase to receive data from Android, but it's not working. Remembering that Firebase is already installed and configured in Android Studio. This is the code:

public class UsersDataBase extends AppCompatActivity {

    EditText EditTextBlue, EditTextRed, EditTextGreen;
    Button save;

    DatabaseReference databaseReference;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cadastrodeuser2);

        databaseReference = FirebaseDatabase.getInstance().getReference("users");

        EditTextBlue = findViewById(R.id.campoblue);
        EditTextRed = findViewById(R.id.campored);
        EditTextGreen = findViewById(R.id.campogreen);

        String blue = EditTextBlue.getText().toString();
        String red = EditTextRed.getText().toString();
        String green = EditTextGreen.getText().toString();

        save = findViewById(R.id.save);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = databaseReference.push().getKey();
                user2 user2 = new user2(blue, red, green);

                databaseReference.child(id).setValue(user2);
            }
        });
    }
}
dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-database:20.0.0'
    implementation 'com.google.firebase:firebase-auth:21.0.1'
    implementation 'com.google.firebase:firebase-firestore:23.0.2'
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
    compile 'com.github.rtoshiro.mflibrary:mflibrary:1.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

}

Please, anyone can help?

Maybe this help you

public class UsersDataBase extends AppCompatActivity {

EditText EditTextBlue, EditTextRed, EditTextGreen;
Button save;

DatabaseReference databaseReference;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cadastrodeuser2);

    databaseReference = FirebaseDatabase.getInstance().getReference("users");

    EditTextBlue = findViewById(R.id.campoblue);
    EditTextRed = findViewById(R.id.campored);
    EditTextGreen = findViewById(R.id.campogreen);

    //you can now remove this lines
    //String blue = EditTextBlue.getText().toString();
    //String red = EditTextRed.getText().toString();
    //String green = EditTextGreen.getText().toString();

    save = findViewById(R.id.save);

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String id = databaseReference.push().getKey();
            user2 user2 = new user2(EditTextBlue.getText().toString(),EditTextRed.getText().toString(), EditTextGreen.getText().toString());

            databaseReference.child(id).setValue(user2);
        }
    });
}

}

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