So I'm building an Android app that adds users to the Firebase database from a form inside the Register Activity.
I'm building this in Android studio 3 and already ran it on my HTC One M9
package com.example.mobilniproekt;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
import java.util.Map;
public class RegisterActivity extends AppCompatActivity {
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Button button=(Button) findViewById(R.id.register);
databaseReference= FirebaseDatabase.getInstance().getReference("users");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText name=(EditText) findViewById(R.id.name);
EditText lastname=(EditText) findViewById(R.id.lastname);
EditText city=(EditText) findViewById(R.id.city);
EditText email=(EditText) findViewById(R.id.email);
EditText username=(EditText) findViewById(R.id.username);
EditText password=(EditText) findViewById(R.id.password);
String id=databaseReference.push().getKey();
User user=new User(id, name.getText().toString(), lastname.getText().toString(), city.getText().toString(), email.getText().toString(), username.getText().toString(), password.getText().toString());
databaseReference.child(id).setValue(user);
name.setText("");
}
});
}
}
However when I launch the app, fill out the form and check my database there isn't any data.
Am I implementing the code wrong or is there another problem with the Firebase configuration?
You need to create a user first for them
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = mAuth.getCurrentUser();
} else {
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
Or if they already have account, they need to sign in first with their details
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = mAuth.getCurrentUser();
}
else {
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
Then you can send to firebase whatever data to write
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.