简体   繁体   中英

how to access child of parent node without affecting other child in that same node?

I have accessed the children of this Users node. But every time I try to update the score and analysis of one child, others will be affected causing the other child to have the same value.

这是Firebase控制台

This is the code i am using to access the children under Users for the score

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    Query query = rootRef.child("Users").orderByChild("username");

    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {        
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    Map<String, Object> map = (Map<String, Object>) ds.getValue();
                    map.put("score", total);
                    ds.getRef().updateChildren(map);

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.d("TAG", databaseError.getMessage());
        }
    };
    query.addListenerForSingleValueEvent(valueEventListener);

This is the code i am using to access the children under Users for the analysis

 DatabaseReference dr = FirebaseDatabase.getInstance().getReference();
        Query q = dr.child("Users").orderByChild("username");

        ValueEventListener vel = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    Map<String, Object> map = (Map<String, Object>) ds.getValue();
                    map.put("analysis", sev);
                    ds.getRef().updateChildren(map);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d("TAG", databaseError.getMessage());
            }
        };
        q.addListenerForSingleValueEvent(vel);
    }

I have to update Analysis and Score without affecting other children

Use equalTo(theusername) to specify which users you are trying to get access-update:

So the query will be:

Query query = rootRef.child("Users").orderByChild("username").equalTo(theusername).child("score");

// or analysis instead of score

And in your listener:

ValueEventListener vel = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {

                   // update the value here ...
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d("TAG", databaseError.getMessage());
            }
        };
        q.addListenerForSingleValueEvent(vel);

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