TheBvrtosz
TheBvrtosz

Reputation: 95

Querying firebase database in android studio with Java

I have a problem with getting the data from the firebase database. I need to collect the data and append it to the array only if the current user Id is present in the participates subdocument.

please see the database structure: Database structure

and here is the code that I'm trying to use to resolve.

private DatabaseReference mDB = FirebaseDatabase.getInstance().getReference().child("events");
private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
private String userid = user.getUid();

mDB.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                try{
                    for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {

                        for(DataSnapshot participant : chidSnap.child("participants").getChildren()){

                            String participantKey = participant.getKey();
                            Log.d(TAG, "onDataChange: " + participantKey);

                            String participantId = (String) participant.child(participantKey).getValue();
                            Log.d(TAG, "onDataChange: " + participantId);

                            if(participantId.equals(userid)){

                                mEventKeys.add(chidSnap.getKey()); //displays the key for the node
                                mEventNames.add((String) chidSnap.child("event_name").getValue());
                                mEventShortDescriptions.add((String) chidSnap.child("event_name").getValue());
                                mEventLongDescriptions.add((String) chidSnap.child("event_name").getValue());
                                mEventCityLocations.add((String) chidSnap.child("event_name").getValue());
                                mEventDates.add((String) chidSnap.child("event_name").getValue());
                                mEventHours.add((String) chidSnap.child("event_name").getValue());
                            }
                        }
                    }
                }catch (Exception e){
                    Log.d(TAG, "onDataChange: " + e.toString());
                }



                initRecyclerView(recyclerView);
            }

I'm trying to grab the key of the single participant (and I'm successfully able to do so) and then insert the downloaded key in order to query the value for the key. When I try to get the value for the particular key I'm receiving a Null value.

Can you guys help?

And also if you have some suggestions regarding how I could improve the data downloading from the firebase I would love to see them! I got a feeling that my approach is not the most optimal.

Upvotes: 0

Views: 72

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 139019

There no need to get the key and use in your reference, you can simply loop twice as in the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference eventsRef = rootRef.child("events");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
            DataSnapshot participantsSnapshot = snapshot.child("participants");
            for(DataSnapshot ds : participantsSnapshot.getChildren()) {
                String uid = ds.getValue(String.class);
                Log.d(TAG, uid);
            }
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
eventsRef.addListenerForSingleValueEvent(valueEventListener);

The result in the logcat will be all uids that exist within the participants array.

Upvotes: 1

Related Questions