Android Firebase读取并更新特定节点,但我无法访问该节点

时间:2018-09-05 11:48:03

标签: android firebase firebase-realtime-database

我正在做一些事情,我必须阅读并增加它的字段,并且当我更新该字段时,它会按下一个新键..我有uid并且我有键,但是我无法在许多情况下更新它我尝试过的方式,这是我的代码:

final DatabaseReference db = FirebaseDatabase.getInstance()
            .getReference(theUID).child("posts");
    db.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists())
            {
                for(DataSnapshot ds : dataSnapshot.getChildren())
                {

                    PostModel postModel = ds.getValue(PostModel.class);
                    int cLikesInt = postModel.getCountAdds();
                    cLikesInt = cLikesInt + 1 ;
                    HashMap<String , Object> hashMap = new HashMap<>();
                    hashMap.put("countAdds" ,cLikesInt);

                    DatabaseReference myRef = FirebaseDatabase.getInstance().getReference(theUID).child("posts");
                    myRef.child(keyPost).updateChildren(hashMap);


                }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

here the structure

0 个答案:

没有答案