1

I am new to Firebase. I have following data in firebase: enter image description here

Inside EventPlayer I have array of data for key eventID. I want to get all objects for a eventID in an Array.

My code:

final String eventId=intent.getStringExtra("EventID");
    mref.child("EventPlayer").child(eventId).orderByChild("eventID").equalTo(eventId);
    // Attach a listener to read the data at our posts reference
    mref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                EventRequest post = child.getValue(EventRequest.class);
                if(post != null){
                    System.out.println(post);
                    arrPlayers.add(post);
                }

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getCode());
        }
    });

The post has no value and it is an empty class.

Edit

Code for creating arrraylist:

arrPlayers = new ArrayList<EventRequest>();
6
  • you are using arrPlayers outside the onDataChange ? Commented Nov 18, 2017 at 20:54
  • HI Bruno, I need to get this class in my arrPlayers and populate listview with it Commented Nov 18, 2017 at 20:57
  • can you post the full code of class and the code to populate listview. Commented Nov 18, 2017 at 21:00
  • Code is complete, I haven't written code to populate listview yet Commented Nov 18, 2017 at 21:09
  • I have added how I am creating arraylist Commented Nov 18, 2017 at 21:10

2 Answers 2

1

Try this code. The only change is in the first few lines, with the postQuery variable.

final String eventId=intent.getStringExtra("EventID");
Query postQuery = mref.child("EventPlayer").child(eventId).orderByChild("eventID").equalTo(eventId);
    // Attach a listener to read the data at our posts reference
    postQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                EventRequest post = child.getValue(EventRequest.class);
                if(post != null){
                    System.out.println(post);
                    arrPlayers.add(post);
                }

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getCode());
        }
    });

Your second line of code creates a Query object, but you never assign it to anything. Assigning it to a new variable and then adding the ValueEventListener to that new variable should get you to your data.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it worked, I am just wondering why it didn't work without Query.
Your original code added the ValueEventListener to a reference that pointed to the root of the database structure. That is not where your data was, so your getValue() call did not return the data you expected. My change saves off the Query object that points to the specific data that you want, then attaches the ValueEventListener to that query so that getValue() returns the data you expect.
Ohh yes I got it now, Thanks for the help Scott
0

To get all those event ids, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference eventIdRef = rootRef.child("EventPlayer").child(eventId);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String eventID = ds.child("eventID").getValue(String.class);
            list.add(eventID);
            Log.d("TAG", eventID);
        }
        Log.d("TAG", list);
    }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.