Tring to populate a list view using a Firebase Query to create a leaderboard

可紊 提交于 2019-12-25 00:13:54

问题


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 ?


回答1:


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):


来源:https://stackoverflow.com/questions/58804970/tring-to-populate-a-list-view-using-a-firebase-query-to-create-a-leaderboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!