0

I am trying to retrieve a list that is on a child that starts with something. Below is a sample of data in my Firebase realtime database:

enter image description here

In the image, I want to retrieve all data that starts with the keyword "jsonmat".

I am using thee code below but it always return null:

DatabaseReference db = FirebaseDatabase.getInstance().getReference()
        .child("Events");
db.startAt("jsonmat").addListenerForSingleValueEvent(
        new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
            Log.i("events", dataSnapshot.toString());
            for (DataSnapshot data : dataSnapshot.getChildren()) {
              // here the user will have the specified email only
            }

          }

          @Override
          public void onCancelled(DatabaseError databaseError){
            Log.i("MyApp", "getUser:onCancelled", databaseError.toException());
          }

        });
Jayson Tamayo
  • 2,741
  • 3
  • 49
  • 76

2 Answers2

0

What you're trying to do isn't possible. You can't order/filter of a nested key, only on direct child keys (-M...) and on nested values (active: true).

Typically you'll want to create a new top-level node, where you store the keys you're searching for, and then the push keys for each matching nested node:

"category_things": {
  "jsonmat_jsonmat": {
    "-M62....uYgB": true,
    "-M62....2-eO": true
  }
}

Also see:

My original, but wrong answer is below...

If you use startAt without specifying an orderBy... clause, the data will be ordered by priority. This priority is a left-over from before Firebase supported ordering on specific properties, so mostly it means that you must call an orderBy... method before filtering.

So:

DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("Events");
db.orderByKey().startAt("jsonmat").addListenerForSingleValueEvent(new ValueEventListener() {
    ...

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

What you can do for your case, is to loop 2 times over the children of the node Events:

//the reference to the node Events

DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("Events");


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

            //loop 1
            for (DataSnapshot data : dataSnapshot.getChildren()) {

                //loop2

             for (DataSnapshot dataTwo : data.getChildren()) {
             //get the key
              String key = dataTwo.getKey();

              if(key.startsWith("jsonmat")){
              //we got a matching key so extract the data and maybe put them in a list

               boolean active = dataTwo.child("active").getValue(Boolean.class);

               int bet = dataTwo.child("bet").getValue(Integer.class);

               String challenger = dataTwo.child("challenger").getValue(String.class); 

               String competitor = dataTwo.child("competitor").getValue(String.class);   

               String game = dataTwo.child("game").getValue(String.class); 

               ............
               ............
               ............     
              }else{
              //we didn't get a match 

              }


             }//end loop2

            }//end loop1

          }

          @Override
          public void onCancelled(DatabaseError databaseError){
            Log.i("MyApp", "getUser:onCancelled", databaseError.toException());
          }

        });
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22