简体   繁体   English

Firebase Android 排行榜系统

[英]Firebase Android Leaderboard system

I have the code:我有代码:

listView = (ListView) findViewById(R.id.list);
    list = new ArrayList<String>();
    arrayAdapter = new ArrayAdapter(getApplicationContext(),  android.R.layout.simple_list_item_1,  list);

    leaderBoard();
}
FirebaseAuth auth = FirebaseAuth.getInstance();

public void leaderBoard(){
    database.getReference().child("db/auth/user/").addValueEventListener(new ValueEventListener() {
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                list = new ArrayList<String>();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    System.out.println("The score is: " + snapshot.child("score").getValue());
                    String s = snapshot.child("name").getValue() + "         " + snapshot.child("score").getValue();
                    list.add(s);

                    arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, list);
                    listView.setAdapter(arrayAdapter);
                }
            }
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());

        }


    });

My goal is to show a list with the users' names and score, so far so good, as shown in the photo.我的目标是显示一个包含用户姓名和分数的列表,到目前为止一切顺利,如图所示。

But now I need the list to show users and points in increasing order based on the score.但现在我需要列表根据分数按升序显示用户和积分。 Can someone help me?有人能帮我吗?

image图片

To order the children by the value of their score property, you can do:要根据score属性的值对子项进行排序,您可以执行以下操作:

database.getReference().child("db/auth/user/").orderByChild("score").addValueEventListener(new ValueEventListener() {
  ...

As far as I can see, the rest of your code can remain the same.据我所知,您的代码的 rest 可以保持不变。

Also see the Firebase documentation on ordering and filtering data .另请参阅有关排序和过滤数据的 Firebase 文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM