简体   繁体   中英

Updating data in firebase realtime database

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:
数据库结构

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM