注册用户 – 添加字段

huangapple go评论83阅读模式
英文:

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,
&quot;Please enter your Name...&quot;, Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(phone)) {
Toast.makeText(TradesmanLoginActivity.this,
&quot;Please enter your Phone Number...&quot;, Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(email)) {
Toast.makeText(TradesmanLoginActivity.this,
&quot;Please enter your Email Address...&quot;, Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(TradesmanLoginActivity.this,
&quot;Please enter a Password...&quot;, Toast.LENGTH_SHORT).show();
} else {
loadingBar.setTitle(&quot;Tradesman Registration&quot;);
loadingBar.setMessage(&quot;Please wait while we check your credentials...&quot;);
loadingBar.show();
Auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(TradesmanLoginActivity.this, new OnCompleteListener&lt;AuthResult&gt;() {
@Override
public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
if (!task.isSuccessful()) {
Toast.makeText(TradesmanLoginActivity.this, &quot;sign up error&quot;, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
} else {
Toast.makeText(TradesmanLoginActivity.this, &quot;Tradesman Registration Successful!&quot;, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
String name = nameField.getText().toString();
String phone= phoneField.getText().toString();
HashMap&lt;String,String&gt; user=new HashMap&lt;&gt;();
user.put(&quot;Name&quot;,name);
user.put(&quot;Phone&quot;,phone);
String user_id = Auth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child(&quot;Users&quot;).child(&quot;Tradesman&quot;).child(user_id).child(&quot;Name&quot;);
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(&quot;Users&quot;).child(&quot;Tradesman&quot;).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&lt;AuthResult&gt;() {
@Override
public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
if (!task.isSuccessful()) {
Toast.makeText(TradesmanLoginActivity.this, &quot;sign up error&quot;, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
} else {
Toast.makeText(TradesmanLoginActivity.this, &quot;Tradesman Registration Successful!&quot;, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
String name = nameField.getText().toString();
String phone= phoneField.getText().toString();
HashMap&lt;String,String&gt; user=new HashMap&lt;&gt;();
user.put(&quot;Name&quot;,name);
user.put(&quot;Phone&quot;,phone);
String user_id = Auth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child(&quot;Users/Tradesman&quot;).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.

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

发表评论

匿名网友

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

确定