0

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) {

        }
    });

here the structure

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Abdelaziz refaat
  • 157
  • 1
  • 12
  • Please check the duplicate to see how you can update a counter with the most common practice, named [Firebase Transactions](https://firebase.google.com/docs/database/android/read-and-write#save_data_as_transactions). – Alex Mamo Sep 05 '18 at 11:53
  • good idea and very simply alex but i can't access to that node even when i have the key – Abdelaziz refaat Sep 05 '18 at 12:37
  • You should make your own attempt given the information in the duplicate answer, and ask another question if something else comes up. – Alex Mamo Sep 05 '18 at 13:00

0 Answers0