java – how to resolve data overriding when getting data from firebase database

java – how to resolve data overriding when getting data from firebase database

To solve this, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classStudentRef = rootRef.child(ClassStudent);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String Studentid = ds.child(Studentid).getValue(String.class);
            Log.d(TAG, Studentid);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
classStudentRef.addListenerForSingleValueEvent(eventListener);

The output will be:

7AnhQ7pRwhXDRqytjSUkoJrOB253
oyvEbB0128Y90ADzXnL0UwCUy0Z2

Edit: To get the name of the student of the coreponding stundet if you need to query your database twice. So, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classStudentRef = rootRef.child(ClassStudent);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String Studentid = ds.child(Studentid).getValue(String.class);
            Log.d(TAG, Studentid);

            DatabaseReference studentIdRef = rootRef.child(Student).child(Studentid);
            ValueEventListener valueEventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String name = ds.child(name).getValue(String.class);
                    Log.d(TAG, name);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {}
            };
            studentIdRef.addListenerForSingleValueEvent(valueEventListener);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
classStudentRef.addListenerForSingleValueEvent(eventListener);

java – how to resolve data overriding when getting data from firebase database

Leave a Reply

Your email address will not be published. Required fields are marked *