How to update a value in a specif key of firebase database

gabriel assenga :

How can I update a value in an existing key in firebase database from android. In my case I want user to update the key Votes

enter image description here

                        final long z=0;
                        Map<String,Object> taskMap = new HashMap<String,Object>();
                        taskMap.put("Votes", z);

                        FirebaseDatabase.getInstance().getReference()
                                .child("project_image").child(projectDate)
                                .child("Votes")
                                .updateChildren(taskMap);

I tried the above codes but instead it add another node

enter image description here

        FirebaseDatabase.getInstance().getReference()
       .child("project_image")
       .child(formattedDate)
       .push().setValue(dataMap);

The above codes were used to insert data at the first time

How can I perform this task

Peter Haddad :

You need to access the key also:

    final long z=0;
 Map<String,Object> taskMap = new HashMap<String,Object>();
 taskMap.put("Votes", z);

FirebaseDatabase.getInstance().getReference().child("project_image")
.child(projectDate).child("-M3zLjPHM_ndBfBDLV9C")
.child("Votes")
.updateChildren(taskMap);

To get the key:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

reference.child("project_image").child(projectDate).addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
   String key = datas.getKey();

   final long z=0;
   Map<String,Object> taskMap = new HashMap<String,Object>();
 taskMap.put("Votes", z);
   FirebaseDatabase.getInstance().getReference().child("project_image")
.child(projectDate).child(key)
.child("Votes")
.updateChildren(taskMap);
    }
 }
   @Override
public void onCancelled(DatabaseError databaseError) {
    throw databaseError.toException();
  }
 });

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400511&siteId=1