注册用户 – 添加字段

huangapple 未分类评论54阅读模式
英文:

Register User - Added fields

问题

  1. Registration.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. // register name/phone/radiogroup
  5. final String name = nameField.getText().toString();
  6. final String phone = phoneField.getText().toString();
  7. final String email = Email.getText().toString();
  8. final String password = Password.getText().toString();
  9. if (TextUtils.isEmpty(name)) {
  10. Toast.makeText(TradesmanLoginActivity.this,
  11. "Please enter your Name...", Toast.LENGTH_SHORT).show();
  12. }
  13. if (TextUtils.isEmpty(phone)) {
  14. Toast.makeText(TradesmanLoginActivity.this,
  15. "Please enter your Phone Number...", Toast.LENGTH_SHORT).show();
  16. }
  17. if (TextUtils.isEmpty(email)) {
  18. Toast.makeText(TradesmanLoginActivity.this,
  19. "Please enter your Email Address...", Toast.LENGTH_SHORT).show();
  20. }
  21. if (TextUtils.isEmpty(password)) {
  22. Toast.makeText(TradesmanLoginActivity.this,
  23. "Please enter a Password...", Toast.LENGTH_SHORT).show();
  24. } else {
  25. loadingBar.setTitle("Tradesman Registration");
  26. loadingBar.setMessage("Please wait while we check your credentials...");
  27. loadingBar.show();
  28. Auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(TradesmanLoginActivity.this, new OnCompleteListener<AuthResult>() {
  29. @Override
  30. public void onComplete(@NonNull Task<AuthResult> task) {
  31. if (!task.isSuccessful()) {
  32. Toast.makeText(TradesmanLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
  33. loadingBar.dismiss();
  34. } else {
  35. Toast.makeText(TradesmanLoginActivity.this, "Tradesman Registration Successful!", Toast.LENGTH_SHORT).show();
  36. loadingBar.dismiss();
  37. String name = nameField.getText().toString();
  38. String phone= phoneField.getText().toString();
  39. HashMap<String,String> user=new HashMap<>();
  40. user.put("Name",name);
  41. user.put("Phone",phone);
  42. String user_id = Auth.getCurrentUser().getUid();
  43. DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Tradesman").child(user_id);
  44. current_user_db.setValue(user);
  45. }
  46. }
  47. });
  48. }
  49. }
  50. });
英文:

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.

  1. Registration.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. // register name/phone/radiogroup
  5. final String name = nameField.getText().toString();
  6. final String phone = phoneField.getText().toString();
  7. final String email = Email.getText().toString();
  8. final String password = Password.getText().toString();
  9. if (TextUtils.isEmpty(name)) {
  10. Toast.makeText(TradesmanLoginActivity.this,
  11. &quot;Please enter your Name...&quot;, Toast.LENGTH_SHORT).show();
  12. }
  13. if (TextUtils.isEmpty(phone)) {
  14. Toast.makeText(TradesmanLoginActivity.this,
  15. &quot;Please enter your Phone Number...&quot;, Toast.LENGTH_SHORT).show();
  16. }
  17. if (TextUtils.isEmpty(email)) {
  18. Toast.makeText(TradesmanLoginActivity.this,
  19. &quot;Please enter your Email Address...&quot;, Toast.LENGTH_SHORT).show();
  20. }
  21. if (TextUtils.isEmpty(password)) {
  22. Toast.makeText(TradesmanLoginActivity.this,
  23. &quot;Please enter a Password...&quot;, Toast.LENGTH_SHORT).show();
  24. } else {
  25. loadingBar.setTitle(&quot;Tradesman Registration&quot;);
  26. loadingBar.setMessage(&quot;Please wait while we check your credentials...&quot;);
  27. loadingBar.show();
  28. Auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(TradesmanLoginActivity.this, new OnCompleteListener&lt;AuthResult&gt;() {
  29. @Override
  30. public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
  31. if (!task.isSuccessful()) {
  32. Toast.makeText(TradesmanLoginActivity.this, &quot;sign up error&quot;, Toast.LENGTH_SHORT).show();
  33. loadingBar.dismiss();
  34. } else {
  35. Toast.makeText(TradesmanLoginActivity.this, &quot;Tradesman Registration Successful!&quot;, Toast.LENGTH_SHORT).show();
  36. loadingBar.dismiss();
  37. String name = nameField.getText().toString();
  38. String phone= phoneField.getText().toString();
  39. HashMap&lt;String,String&gt; user=new HashMap&lt;&gt;();
  40. user.put(&quot;Name&quot;,name);
  41. user.put(&quot;Phone&quot;,phone);
  42. String user_id = Auth.getCurrentUser().getUid();
  43. DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child(&quot;Users&quot;).child(&quot;Tradesman&quot;).child(user_id).child(&quot;Name&quot;);
  44. current_user_db.setValue(email);
  45. }
  46. }
  47. });
  48. }
  49. }
  50. });

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 处放入了错误的值。用以下内容替换:

  1. DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Tradesman").child(user_id);
  2. current_user_db.setValue(user);
英文:

You put the wrong value at setValue. Replace with this.

  1. DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child(&quot;Users&quot;).child(&quot;Tradesman&quot;).child(user_id);
  2. current_user_db.setValue(user);

答案2

得分: 1

如果您想在 /Users/Tradesman/$uid 下创建一个节点,其中包含用户的姓名和电话号码,那么代码如下所示:

  1. Auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(TradesmanLoginActivity.this, new OnCompleteListener<AuthResult>() {
  2. @Override
  3. public void onComplete(@NonNull Task<AuthResult> task) {
  4. if (!task.isSuccessful()) {
  5. Toast.makeText(TradesmanLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
  6. loadingBar.dismiss();
  7. } else {
  8. Toast.makeText(TradesmanLoginActivity.this, "Tradesman Registration Successful!", Toast.LENGTH_SHORT).show();
  9. loadingBar.dismiss();
  10. String name = nameField.getText().toString();
  11. String phone = phoneField.getText().toString();
  12. HashMap<String, String> user = new HashMap<>();
  13. user.put("Name", name);
  14. user.put("Phone", phone);
  15. String user_id = Auth.getCurrentUser().getUid();
  16. DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users/Tradesman").child(user_id);
  17. current_user_db.setValue(user);
  18. }
  19. }
  20. });

最大的变化是之前只有在其中写入了 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:

  1. Auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(TradesmanLoginActivity.this, new OnCompleteListener&lt;AuthResult&gt;() {
  2. @Override
  3. public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
  4. if (!task.isSuccessful()) {
  5. Toast.makeText(TradesmanLoginActivity.this, &quot;sign up error&quot;, Toast.LENGTH_SHORT).show();
  6. loadingBar.dismiss();
  7. } else {
  8. Toast.makeText(TradesmanLoginActivity.this, &quot;Tradesman Registration Successful!&quot;, Toast.LENGTH_SHORT).show();
  9. loadingBar.dismiss();
  10. String name = nameField.getText().toString();
  11. String phone= phoneField.getText().toString();
  12. HashMap&lt;String,String&gt; user=new HashMap&lt;&gt;();
  13. user.put(&quot;Name&quot;,name);
  14. user.put(&quot;Phone&quot;,phone);
  15. String user_id = Auth.getCurrentUser().getUid();
  16. DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child(&quot;Users/Tradesman&quot;).child(user_id);
  17. current_user_db.setValue(user);
  18. }
  19. }
  20. });

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.

huangapple
  • 本文由 发表于 2020年4月9日 05:26:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61110295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定