0

I can't seem to populate a list view when I use a Query. I can populate it fine by just passing in all Users, although i am trying to create a leaderboard so I need to order these users.

when I use the following code the list view populates fine but of course not ordered:

    database = FirebaseDatabase.getInstance().getReference().child("Users");
    listview = (ListView) findViewById(R.id.Leaderboard);

    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,leaderboardlist);
    listview.setAdapter(adapter);
    database.addChildEventListener(new ChildEventListener()

I then tried this code to order it:

database = FirebaseDatabase.getInstance().getReference().child("Users");
        listview = (ListView) findViewById(R.id.Leaderboard);

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,leaderboardlist);
        listview.setAdapter(adapter);
        Query queryRef = database.orderByValue().limitToFirst(10);

       queryRef.addChildEventListener(new ChildEventListener()

This returned the same list view (populated but no order). I am attempting to order the users by their score value, so I changed my query to:

    Query queryRef = database.child("score").orderByValue().limitToFirst(10);

Although when I did this it seems not to work, the list view doesn't populate.

It won't let me export my JSON file of the database so can only attach the tree to help give you an idea what the uUser looks like.

Database tree

Inside the ChildListener I have this:

 @Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s)

{

 String user = dataSnapshot.getValue(User.class).toString();

                leaderboardlist.add(user);

                adapter.notifyDataSetChanged();
            }

So I am not too sure where I have gone wrong or how I can fix this

Just Incase anyone is wondering I ended up fixing this,

the correct way to query isn't ordering by value but ordering by child:

Eg: instead of

Query queryRef = database.child("score").orderByValue().limitToFirst(10);

it should :


Query queryRef = database.orderByChild("score").limitToFirst(10);

My problem now is that it is now ordering them in reverse,

I tried to just change it from

limitToFirst(10);

to

limitToLast(10):

But this didnt change a thing ?

robsiemb
  • 6,157
  • 7
  • 32
  • 46
  • Hi Chris, and welcome to SO. You shouldn't answer your question by editing the question text -- you should answer it as an _answer_ (and then accept that answer -- after the 2 day grace period) so that future readers can see it. If you have a new question, ask a new question. – robsiemb Nov 11 '19 at 17:46
  • However, I think this should be the answer to your second question: https://stackoverflow.com/questions/34156996/firebase-data-desc-sorting-in-android -- note that since you have 8 items in your database, `limitToFirst(10)` and `limitToLast(10)` will return exactly the same results. – robsiemb Nov 11 '19 at 17:54
  • Hi @robsiemb Thank you, I have 11 in my database now and it does display the top 10 eliminating the last one but when displayed it is always in the order lowest to highest for both limitToFirst(10) and limitToLast(10). Although this wasn't the original question so have posted the answer to the original question – Chris Kavanagh Nov 11 '19 at 18:08
  • Firebase only sorts in ascending order. See the question I linked for solutions. – robsiemb Nov 11 '19 at 18:09

1 Answers1

0

The correct way to query isn't ordering by value but ordering by child: Eg: instead of

Query queryRef = database.child("score").orderByValue().limitToFirst(10);

it should :

Query queryRef = database.orderByChild("score").limitToFirst(10);

To make sure you have it ordered in the correct way you have to reverse order so instead of:

limitToFirst(10);

it should be:

limitToLast(10):