英文:
Register User - Added fields
问题
Registration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// register name/phone/radiogroup
final String name = nameField.getText().toString();
final String phone = phoneField.getText().toString();
final String email = Email.getText().toString();
final String password = Password.getText().toString();
if (TextUtils.isEmpty(name)) {
Toast.makeText(TradesmanLoginActivity.this,
"Please enter your Name...", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(phone)) {
Toast.makeText(TradesmanLoginActivity.this,
"Please enter your Phone Number...", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(email)) {
Toast.makeText(TradesmanLoginActivity.this,
"Please enter your Email Address...", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(TradesmanLoginActivity.this,
"Please enter a Password...", Toast.LENGTH_SHORT).show();
} else {
loadingBar.setTitle("Tradesman Registration");
loadingBar.setMessage("Please wait while we check your credentials...");
loadingBar.show();
Auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(TradesmanLoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(TradesmanLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
} else {
Toast.makeText(TradesmanLoginActivity.this, "Tradesman Registration Successful!", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
String name = nameField.getText().toString();
String phone= phoneField.getText().toString();
HashMap<String,String> user=new HashMap<>();
user.put("Name",name);
user.put("Phone",phone);
String user_id = Auth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Tradesman").child(user_id);
current_user_db.setValue(user);
}
}
});
}
}
});
英文:
So I am trying to register a user for my android app, currently it uses the Firebase authentication to register using a email and password. But I am trying to add a name and phone field to register with too.
Registration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// register name/phone/radiogroup
final String name = nameField.getText().toString();
final String phone = phoneField.getText().toString();
final String email = Email.getText().toString();
final String password = Password.getText().toString();
if (TextUtils.isEmpty(name)) {
Toast.makeText(TradesmanLoginActivity.this,
"Please enter your Name...", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(phone)) {
Toast.makeText(TradesmanLoginActivity.this,
"Please enter your Phone Number...", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(email)) {
Toast.makeText(TradesmanLoginActivity.this,
"Please enter your Email Address...", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(TradesmanLoginActivity.this,
"Please enter a Password...", Toast.LENGTH_SHORT).show();
} else {
loadingBar.setTitle("Tradesman Registration");
loadingBar.setMessage("Please wait while we check your credentials...");
loadingBar.show();
Auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(TradesmanLoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(TradesmanLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
} else {
Toast.makeText(TradesmanLoginActivity.this, "Tradesman Registration Successful!", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
String name = nameField.getText().toString();
String phone= phoneField.getText().toString();
HashMap<String,String> user=new HashMap<>();
user.put("Name",name);
user.put("Phone",phone);
String user_id = Auth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Tradesman").child(user_id).child("Name");
current_user_db.setValue(email);
}
}
});
}
}
});
I've created the fields etc, I just am struggling with the part of putting the name and phone into the firebase database.
Currently it registers a user and sets the Name to the users email. I want to store the name and phone as children of the user ID(which they enter when registering), rather than just the email
答案1
得分: 2
你在 setValue
处放入了错误的值。用以下内容替换:
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Tradesman").child(user_id);
current_user_db.setValue(user);
英文:
You put the wrong value at setValue
. Replace with this.
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Tradesman").child(user_id);
current_user_db.setValue(user);
答案2
得分: 1
如果您想在 /Users/Tradesman/$uid
下创建一个节点,其中包含用户的姓名和电话号码,那么代码如下所示:
Auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(TradesmanLoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(TradesmanLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
} else {
Toast.makeText(TradesmanLoginActivity.this, "Tradesman Registration Successful!", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
String name = nameField.getText().toString();
String phone = phoneField.getText().toString();
HashMap<String, String> user = new HashMap<>();
user.put("Name", name);
user.put("Phone", phone);
String user_id = Auth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users/Tradesman").child(user_id);
current_user_db.setValue(user);
}
}
});
最大的变化是之前只有在其中写入了 email
,而上面的更新会将包含姓名和电话号码的完整映射写入节点。
英文:
If you want to create a node under /Users/Tradesman/$uid
with the name and phone number of the user, that'd look like this:
Auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(TradesmanLoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(TradesmanLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
} else {
Toast.makeText(TradesmanLoginActivity.this, "Tradesman Registration Successful!", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
String name = nameField.getText().toString();
String phone= phoneField.getText().toString();
HashMap<String,String> user=new HashMap<>();
user.put("Name",name);
user.put("Phone",phone);
String user_id = Auth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users/Tradesman").child(user_id);
current_user_db.setValue(user);
}
}
});
The biggest change is that you were only writing email
in there, and the update above writes the entire map with both name and phone number.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论