【问题标题】:Firebase: Add to Authenticated UserFirebase:添加到经过身份验证的用户
【发布时间】:2018-08-18 15:36:48
【问题描述】:

请帮忙,我可以为我的认证用户添加值吗?

我有一个 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 值吗?

让经过身份验证的用户可以查看他们已输入的孩子姓名?谢谢

谢谢

【问题讨论】:

    标签: android firebase firebase-realtime-database android-edittext firebase-authentication


    【解决方案1】:

    是的,在您对用户进行身份验证后,您应该获得您的用户Auth ID

    如果您的用户成功登录到您的应用,您应该这样做以将孩子放入您的用户中

    private FirebaseAuth mAuth;
    String userID;
    
    // ...
    mAuth = FirebaseAuth.getInstance();
    userID = mAuth.getCurrentUser().getUid();
    

    之后,在您发布 addChildren 类的地方,您应该调用您的用户以成功地将数据添加到未经身份验证的用户

    mDatabase.child(userID).child("Name").setValue("Robert");
    

    这是一个关于如何在您的身份验证用户中添加数据的示例。

    mDatabase 是你的数据库引用,像这样

    mDatabase = FirebaseDatabase.getInstance().getReference();
    

    快乐编码

    【讨论】:

    • 谢谢我还是有点困惑——我要不要添加私有 FirebaseAuth mAuth;字符串用户ID; // ... mAuth = FirebaseAuth.getInstance(); userID = mAuth.getCurrentUser().getUid();到我的 addChild 类?
    • 'String userID' 是孩子的名字吗?
    • Userid 将是每个登录您应用的用户的 ID,因此数据将在每个用户登录时保存
    • 我知道我应该把这个放在哪里?private FirebaseAuth mAuth;字符串用户ID; // ... mAuth = FirebaseAuth.getInstance(); userID = mAuth.getCurrentUser().getUid();在 AddChilld 中?
    • 所以我不把它放在上面?
    猜你喜欢
    • 2017-12-27
    • 2019-09-27
    • 1970-01-01
    • 2016-01-17
    • 2013-08-20
    • 1970-01-01
    • 2020-11-15
    • 2018-01-07
    • 2018-10-10
    相关资源
    最近更新 更多