如何使用onClick创建具有firebase用户的UID的新子项作为子项的名称?

时间:2018-01-07 11:20:30

标签: android firebase firebase-realtime-database

FirebaseDatabase database = FirebaseDatabase.getInstance();

    final DatabaseReference uRef = database.getReference("orders");
    final DatabaseReference mRef = database.getReference("orders");
    final DatabaseReference nameRef = database.getReference("orders").child("username");
    final DatabaseReference eRef = database.getReference("orders").child("Google Email");
    final DatabaseReference emailRef = database.getReference("orders").child("User Email");
    final DatabaseReference nRef = database.getReference("orders").child("Google Number");
    final DatabaseReference numberRef = database.getReference("orders").child("User Number");

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            submitForm();


            String uidValue = Utils.UID;
            uRef.setValue(uidValue);

            String nameValue = Utils.USERNAME;
            mRef.setValue(nameValue);

            String userNameValue = inputName.getText().toString();
            nameRef.setValue(userNameValue);

            String emailValue = Utils.EMAIL;
            eRef.setValue(emailValue);

            String userEmailValue = inputEmail.getText().toString();
            emailRef.setValue(userEmailValue);

            String numberValue = Utils.PHONENO;
            nRef.setValue(numberValue);

            String userNumberValue = inputPhoneNumber.getText().toString();
            numberRef.setValue(userNumberValue);
        }
    });

}

数据库描述

给定代码仅创建一个新子项,之后每次使用onclick时代码将使用新代码替换已存在的子代,而不是使用UID创建新子项

1 个答案:

答案 0 :(得分:4)

为了保持代码清洁,请将您的数据放入HashMap,然后在setValue之前使用 PUSH

Map<String, String> user_info = new HashMap<String, String>();

user_info.put("id", Utils.UID);
user_info.put("username", Utils.USERNAME);
user_info.put("Google Email", Utils.EMAIL);
......

mRef.push().setValue(user_info);