无法使用userId

时间:2018-04-27 11:35:11

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

在这里,我使用'firstName','lastName','email','password'在注册过程中创建一个'Firebase'用户。成功登录后,我将所有注册值加载到另一个屏幕上,新的两个“Edittext”。现在我需要通过'Firebase''userId'更新该注册表,其中新的两个值'country'和'job'在new'中输入EditText'。为了执行该注册过程并添加新值,我使用了“UserProfileBean”类。

下面是代码。

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("new_user");
        FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
        String userid=user.getUid();


        final UserProfileBean userProfileBean = new UserProfileBean();
        userProfileBean.setCountry(country.getText().toString());
        userProfileBean.setOccupation(occupation.getText().toString());

        Log.d("UPDATE_PROFILE", "UPDATE_PROFILE_DATA" + userid + "," +
                userProfileBean.getOccupation() + ", " + userProfileBean.getCountry());

        //myRef.child(userid).child("country").setValue(userProfileBean.getCountry());

        Query pendingTasks = myRef.child(userid).orderByChild("country").equalTo("");
        pendingTasks.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot tasksSnapshot) {
                for (DataSnapshot snapshot: tasksSnapshot.getChildren()) {
                    snapshot.getRef().child("country").setValue(userProfileBean.getCountry());
                    snapshot.getRef().child("occupation").setValue(userProfileBean.getOccupation());
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println("The read failed: " + databaseError.getMessage());
            }
        });

上面的代码是通过按钮'onclickListener'实现的。

1 个答案:

答案 0 :(得分:1)

要更新用户节点,请尝试以下操作:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String useruid=user.getUid();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("User").child(useruid);

 Map<String,Object> update=new HashMap<>();
 update.put("Country",userProfileBean.getCountry());
 update.put("Occupation",userProfileBean.getOccupation());

ref.updateChildren(update);

更多信息:

https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields