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.
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 ?