I am working on something that i have to read the field of and increment it and when i update the field it push a new key .. i had the uid and i had the key but i can't update it in many ways i tried and this is my code :
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) {
}
});