请帮忙,我可以为经过身份验证的用户添加值吗?
我有一个createUser类,我在其中创建了经过身份验证的用户。
public class CreateUserAccount extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private Button btnSignUp;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
btnSignUp = (Button) findViewById(R.id.sign_up_button);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
Toast.makeText(this, "Please turn on usage access for this app", Toast.LENGTH_LONG).show();
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter Email Address", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter Password", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password is too short. Please enter a minimum of 6 characters", Toast.LENGTH_SHORT).show();
return;
}
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(CreateUserAccount.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(CreateUserAccount.this, "User Account Created" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(CreateUserAccount.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(CreateUserAccount.this, LandingPage.class));
finish();
}
}
});
}
});
}
}
经过身份验证的用户登录后,可以单击可以输入子项名称的页面,这些名称将写入实时数据库。
public class AddChild extends AppCompatActivity {
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String useruid=user.getUid();
//we will use these constants later to pass the artist name and id to another activity
public static final String ARTIST_NAME = "net.simplifiedcoding.firebasedatabaseexample.artistname";
public static final String ARTIST_ID = "net.simplifiedcoding.firebasedatabaseexample.artistid";
//view objects
EditText editChildName;
CardView addChild;
ListView lvChildren;
//a list to store all the artist from firebase database
List<Child> children;
//our database reference object
DatabaseReference databaseChildren;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_child);
//getting the reference of artists node
databaseChildren = FirebaseDatabase.getInstance().getReference("children");
//getting views
editChildName = (EditText) findViewById(R.id.editChildName);
lvChildren = (ListView) findViewById(R.id.lvChildren);
addChild = (CardView) findViewById(R.id.addChild);
//list to store artists
children = new ArrayList<>();
//adding an onclicklistener to button
addChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//calling the method addArtist()
//the method is defined below
//this method is actually performing the write operation
addChild();
}
});
}
/*
* This method is saving a new artist to the
* Firebase Realtime Database
* */
private void addChild() {
//getting the values to save
String name = editChildName.getText().toString().trim();
//checking if the value is provided
if (!TextUtils.isEmpty(name)) {
//getting a unique id using push().getKey() method
//it will create a unique id and we will use it as the Primary Key for our Artist
String id = databaseChildren.push().getKey();
//creating an Artist Object
Child child = new Child(name);
//Saving the Artist
databaseChildren.child(id).setValue(child);
//setting edittext to blank again
editChildName.setText("");
//displaying a success toast
Toast.makeText(this, "Child added", Toast.LENGTH_LONG).show();
} else {
//if the value is not given displaying a toast
Toast.makeText(this, "Please enter a name", Toast.LENGTH_LONG).show();
}
}
}
______儿童类________
import com.google.firebase.database.IgnoreExtraProperties;
@IgnoreExtraProperties
public class Child {
private String childName;
public Child(){
//this constructor is required
}
public Child(String childName) {
this.childName = childName;
}
public String getChildName() {
return childName;
}
}
有没有办法添加我输入给该用户的addChild类db值?
以便经过身份验证的用户可以查看已输入的子女姓名?感谢
由于
答案 0 :(得分:0)
是的,在您对用户进行身份验证后,您应该获得用户private FirebaseAuth mAuth;
String userID;
// ...
mAuth = FirebaseAuth.getInstance();
userID = mAuth.getCurrentUser().getUid();
ID
如果您的用户成功登录到您的应用程序,您应该这样做以将孩子放入您的用户
mDatabase.child(userID).child("Name").setValue("Robert");
之后,在您发布addChildren类的地方,您应该呼叫您的用户成功地将数据添加到擅长的用户
mDatabase = FirebaseDatabase.getInstance().getReference();
这是一个关于如何在auth用户中添加数据的示例。
其中mDatabase是您的数据库引用,如此
{{1}}
快乐编码