我想创建一个按钮,当用户点击它时,它会自动从firebase数据库中检索用户名。然后,它将存储到另一个数据库中。就像有人在预订项目一样。最后,它将在页面上显示已经预订服务的用户名。
这是blogsingleactivity java文件。
public class BlogSingleActivity extends AppCompatActivity {
private Button mSubmitBook;;
private DatabaseReference mDatabaseBookUser;
private FirebaseAuth firebaseAuth;
List<Blog> Info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blog_single);
firebaseAuth= FirebaseAuth.getInstance();
mSubmitBook = (Button) findViewById(R.id.SubmitBook);
mDatabaseBookUser = FirebaseDatabase.getInstance().getReference("Users");
Info = new ArrayList<>();
mSubmitBook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(view== mSubmitBook){
addName();
}
}
});
}
此页面是注册java文件
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button buttonRegister;
private EditText mMatricField;
private EditText mNameField;
private EditText editTextEmail;
private EditText editTextPassword;
private TextView textViewSignin;
private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth= FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser() !=null){
//profile activity here
finish();
startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
}
mDatabase= FirebaseDatabase.getInstance().getReference().child("Users");
progressDialog=new ProgressDialog(this);
buttonRegister=(Button) findViewById(R.id.buttonRegister);
mMatricField= (EditText) findViewById(R.id.matricField);
mNameField=(EditText) findViewById(R.id.nameField);
editTextEmail=(EditText) findViewById(R.id.editTextEmail);
editTextPassword=(EditText) findViewById(R.id.editTextPassword);
textViewSignin=(TextView) findViewById(R.id.textViewSignin);
buttonRegister.setOnClickListener(this);
textViewSignin.setOnClickListener(this);
}
private void registerUser(){
final String matric=mMatricField.getText().toString().trim();
final String name= mNameField.getText().toString().trim();
String email=editTextEmail.getText().toString().trim();
String password=editTextPassword.getText().toString().trim();
if(TextUtils.isEmpty(matric)){
//password is empty
Toast.makeText(this, "Please enter matric number", Toast.LENGTH_SHORT).show();
//stopping the function execution further
return;
}
if(TextUtils.isEmpty(name)){
//password is empty
Toast.makeText(this, "Please enter your name", Toast.LENGTH_SHORT).show();
//stopping the function execution further
return;
}
if(TextUtils.isEmpty(email)){
//email is empty
Toast.makeText(this,"Please enter email",Toast.LENGTH_SHORT).show();
//stopping the function execution further
return;
}
if(TextUtils.isEmpty(password)){
//password is empty
Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
//stopping the function execution further
return;
}
//if validation are ok
//we will first show a progressbar
progressDialog.setMessage("Registering User...");
progressDialog.show();
firebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
String user_id=firebaseAuth.getCurrentUser().getUid();
DatabaseReference current_user_db= mDatabase.child(user_id);
current_user_db.child("name").setValue(name);
current_user_db.child("matric").setValue(matric);
current_user_db.child("image").setValue("default");
//user is successfully registered and logged in
//we will start the profile activity here
//display a toast only
finish();
startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
Toast.makeText(MainActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Could not register... Please try again", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onClick(View view) {
if(view== buttonRegister){
registerUser();
}
if(view== textViewSignin){
//will open login activity here
startActivity(new Intent(this, LoginActivity.class));
}
}
}
答案 0 :(得分:0)
我相信您需要使用FirebaseOptions.Builder
,其中您为两个&#34; Firebase项目&#34;保留了两个不同的参考资料。
有关详细信息,请参阅https://firebase.google.com/docs/configure/。