0

I am trying to query an event by the user's unique id, but it won't return anything. Here's my code for the query:

Query events = mDatabaseEvents.orderByValue().equalTo("user1");

and here's my Firebase Database structure:

events: {
  -event1: {
  1: user1,
  2: user2,
  3: user3,
},
  -event2: {
  1: user4,
  2: user5,
  3: user1,
},

I believe it is not working because the data is set through a hashmap, so "user1" may not be always in the same spot in each event. How do I go about fixing this problem? Thanks

Spotz
  • 113
  • 3
  • 8
  • Please share your code that you tried so far, and post your firebase database structure. – Alex Mamo Sep 28 '17 at 11:20
  • Nothing about your current code actually does anything with the Firebase Database yet. It merely defines a query. If you want to retrieve the matching items from the database, you'll have to [attach a listener](https://firebase.google.com/docs/database/android/lists-of-data#child-events). – Frank van Puffelen Sep 28 '17 at 13:26

1 Answers1

0

You need to add an "addValueEventListener" to actually get the data. Here is an example with "orderByKey", there will be little changes i suppose:

Query query = locationComment.orderByKey();
query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.getValue() == null) {
            return;
        }
        data.clear();
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            ...
        }
        adapter.notifyDataSetChanged();
    }

You can see more info here: https://stackoverflow.com/a/42430304/5309996