java - 任务成功后附加参数未存储在 Firebase 数据库中?

标签 java android firebase firebase-realtime-database firebase-authentication

这是我的 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代码是 `应用插件:'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'`

或者您可以指定任何其他方式,让我可以同时添加所有数据数据的包括(姓名,电子邮件ID,密码,电话号码)

最佳答案

试试这个,因为这是注册而不是登录:

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

关于java - 任务成功后附加参数未存储在 Firebase 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147698/

相关文章:

java - PreparedStatement 带参数的最大查询未在 Java 中执行

android - 如何跳过 firestore 集合中的 null/空变量?

android - 错误 :Unexpected lock protocol found in lock file. 预期 3,找到 0。

javascript - Firebase 电话身份验证和链接

Android从定义的位置查找X点的纬度经度

java - Spring Data Native 查询 Lob 列的有趣错误

java - Jsoup 读取超时取决于 UserAgent 字符串

java - 使用 Java 泛型的包装层次结构

android - 在 Activity 中隐藏图标和标签(启动画面)

android - 如何从 Assets 中异步加载大量文件?