这是我的Register.java页面。这里我无法在数据库中存储姓名和电话号码。当我尝试存储时没有响应。
public class Register extends AppCompatActivity {
private EditText mEmail;
private EditText mPassword;
private EditText mPhone;
private EditText mName;
private FirebaseAuth mAuth;
private ProgressBar progressBar;
DatabaseReference mData;
public void register(View v)
{
startRegister();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
mEmail = (EditText)findViewById(R.id.email);
mPassword = (EditText) findViewById(R.id.password);
mPhone = (EditText) findViewById(R.id.phone);
mName = (EditText) findViewById(R.id.name);
mData=FirebaseDatabase.getInstance().getReference().child("Users");
progressBar = new ProgressBar(Register.this,null,android.R.attr.progressBarStyleLarge);
RelativeLayout layout = findViewById(R.id.layout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);
progressBar.setVisibility(View.INVISIBLE);
}
private void startRegister(){
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
final String phone = mPhone.getText().toString();
final String name = mName.getText().toString();
if(!TextUtils.isEmpty(password)&&!TextUtils.isEmpty(phone)&&!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(email)){
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(Register.this,new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mData.child(user_id);
current_user_db.child("name").setValue(name);
current_user_db.child("phone").setValue(phone);
//mName.getText().toString() mPhone.getText().toString()
// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
// progressBar.setVisibility(View.GONE);
// Intent main =new Intent(Register.this,MainActivity.class);
// startActivity(main);
}
}
});
}
}
}
我的gradle代码是 `apply plugin:'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.example.ashwanths.helpwithfood"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.firebase:firebase-auth:11.8.0'
compile 'com.google.firebase:firebase-database:11.8.0'
compile 'com.google.android.gms:play-services-maps:11.8.0'
compile 'com.google.firebase:firebase-messaging:11.8.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'`
或者您可以指定我可以同时添加所有数据的其他方式数据包括(姓名,emailId,密码,电话号码)
答案 0 :(得分:1)
试试这个,因为这是注册而不是登录:
FirebaseUser users;
if(task.isSuccessful()){
users= task.getResult().getUser();
DatabaseReference ref = mData.child(users.getUid());
ref.child("name").setValue(name);
ref.child("phone").setValue(phone);
然后你将拥有数据库:
Users
useruid
name: name_here
phone: phone_here