1

I am having trouble getting data from my database.

My database: enter image description here

My code:

 Query query = FirebaseDatabase.getInstance().getReference("Quotes").startAt(5).endAt(10);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot data: dataSnapshot.getChildren()){
                        String authorName = data.child("Name").getValue().toString();
                        String quoteGiven = data.child("Quote").getValue().toString();
                        name.setText("- " + authorName);
                        quote.setText(quoteGiven);

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(MainActivity.this, "Loading Quote failed", Toast.LENGTH_SHORT).show();
            }
        });

I want to get my data from the 5th child to the 10th child.

I've tried:

Select random entry from Firebase Realtime Database in Java

get random value android firebase java

Firebase queries not working on Android even though the reference retrieves data

However, the is only showing the hard-coded text I left and not the text from the database?

can anyone help me, I am not sure where i went wrong

AHPV
  • 77
  • 7

2 Answers2

3

Try using orderByKey():

FirebaseDatabase.getInstance().getReference("Quotes").orderByKey().startAt(String.valueOf(5)).endAt(String.valueOf(10));

This should give you all the data from the 5th key to the 10th in your database.

From the docs:

orderByKey()

Order results by child keys

https://firebase.google.com/docs/database/android/lists-of-data#sort_data

Community
  • 1
  • 1
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • I've added, this and it was still not showing any data. I then added a toast message on the for loop and it did not trigger, am i doing something wrong in the event listener as its not going into the for loop?? – AHPV Dec 16 '19 at 10:18
  • Can u add a log before the for loop, also remove startat and endAt, and test it – Peter Haddad Dec 16 '19 at 10:19
  • Caused by: java.lang.IllegalArgumentException: You must use startAt(String value), endAt(String value) or equalTo(String value) in combination with orderByKey(). Other type of values or using the version with 2 parameters is not supported – AHPV Dec 16 '19 at 10:20
  • fixed it, ```FirebaseDatabase.getInstance().getReference("Quotes").orderByKey().startAt(String.valueOf(5)).endAt(String.valueOf(10));``` – AHPV Dec 16 '19 at 10:22
2

To solve this, please change the following line of code:

Query query = FirebaseDatabase.getInstance().getReference("Quotes").startAt(5).endAt(10);

to

Query query = FirebaseDatabase.getInstance().getReference("Quotes")
    .orderByKey().startAt("5").endAt("10");

The keys that you pass to the startAt() and endAt() methods, should always be strings and not numbers.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193