Updating data in firebase realtime database

Viewed 58

I want to update the above record but when I update that it updates the parent (user) instead of the child.

  `     button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendNotification();
            Toast.makeText(MainActivity2.this, str, Toast.LENGTH_SHORT).show();
            HashMap<String, Object> m = new HashMap<String, Object>();
            Intent intent = getIntent();
            String ID=intent.getStringExtra("counter");
            String email = intent.getStringExtra("email");
            m.put("ID",ID);
            m.put("Email", email);
            if(s!="Query" && s!="Resource")
            m.put("Complain", t.getText().toString());
            else if(s!="Complain" && s!="Resource")
            m.put("Query", t.getText().toString());
            else
            m.put("Resources",t.getText().toString());
            m.put("Time", "");
            m.put("Feedback", "Pending");
            FirebaseDatabase.getInstance().getReference().child("User").updateChildren(m).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void unused) {
                    Toast.makeText(MainActivity2.this, "Your data is successfully updated", Toast.LENGTH_SHORT).show();
                }
            });

        }
    });`

DB structure:
DB structure

1 Answers

I want to update the above record but when I update that it updates the parent (user) node instead of the child.

That's the expected behavior since your calling updateChildren() on the following reference:

FirebaseDatabase.getInstance().getReference().child("User");

Which points exactly to the User node. If you want to update the child, you have to add the key to the reference:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference childRef = db.child("User").child("-N2gd...8-6b"); 
childRef.updateChildren(m).addOnSuccessListener(/* ... /*);

So the key to solving this problem is to add the key of the node in your reference. If you didn't store the key yet, then please check the answer from the following post:

Related