Carl Bruiners
Carl Bruiners

Reputation: 546

Simple Android / Firebase orderByChild / equalTo

This I suspect is easy, but I can't get it to work as I want. I'm referencing a database ref with my query information for my Firebase database. The code below works fine, but I can't hard code in Match_01 (this was purely done to get the code working).

String getArgument = getArguments().getString("matchid");
            final DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Matches").child("Match_01");

What I need to do is use the matchID thats been passed to the fragment and use equalTo instead of referencing the final child node.

final DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Matches").orderByChild("gameID").equalTo(getArgument);

But this doesn't work, I can't swap out the last child reference for the orderByChild reference.

All help is appreciated.

Upvotes: 0

Views: 952

Answers (1)

VonD
VonD

Reputation: 5155

I'm assuming from your question that the Matches node's children have the id you want to reference dynamically as a key.

Then, you need orderByKey instead of orderByChild. This should work:

final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Matches");
Query q=ref.orderByKey().equalTo(getArgument);

When using orderByChild, the query will match nodes against this node's attributes. In other words, your attempt would work on a collection with the following structure:

- Matches
  - $key
    - gameID: "Match_01"

Upvotes: 1

Related Questions