简体   繁体   English

如何在 Firebase 中创建排行榜?

[英]How can I create a ranked leaderboard in Firebase?

There is a point system in my application.我的应用程序中有一个积分系统。 I want to create an ordered list of points in a part.我想在零件中创建一个有序的点列表。 It will be in order from highest score to lowest score.它将按从最高分到最低分的顺序排列。 In the code I wrote, this system does not work correctly.在我写的代码中,这个系统不能正常工作。 not ranked from highest to lowest score.没有从最高分到最低分排名。 Newly rated users appear at the top.新评价的用户出现在顶部。 How can I fix this problem?我该如何解决这个问题? I want it sorted from highest score to lowest score.我希望它从最高分到最低分排序。

Scoreboard class:记分牌 class:

DatabaseReference UsersRef, RatingsRef;
int currentpage = 1 ;
static final int total_ITEMS = 10 ;
RecyclerView scorrecy ;

UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
RatingsRef = FirebaseDatabase.getInstance().getReference().child("Ratings");
RatingsRef.keepSynced(false);

getScor();

  public void getScor(){

        FirebaseRecyclerOptions<ScorModel> options =
                new FirebaseRecyclerOptions.Builder<ScorModel>()
                        .setQuery(RatingsRef.orderByChild("puan").limitToLast(currentpage * total_ITEMS),ScorModel.class)
                        .build();

        FirebaseRecyclerAdapter<ScorModel,ScorViewHolder> adapter
                =new FirebaseRecyclerAdapter<ScorModel, ScorViewHolder>(options)
        {

            @Override
            protected void onBindViewHolder(@NonNull final ScorBoard.ScorViewHolder holder, int position, @NonNull ScorModel model) {
                final String visit_user_id = getRef(position).getKey();  

                UsersRef.child(visit_user_id).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            if (dataSnapshot.hasChild("isim")){
                                final String myUsername = dataSnapshot.child("isim").getValue().toString();
                                holder.userName.setText(myUsername);
                            }

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
                RatingsRef.child(visit_user_id).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot.hasChild("puan")){
                            final String myPoint = dataSnapshot.child("puan").getValue().toString();
                            holder.userPoint.setText(myPoint+" "+"Puan");
                        }else  {
                            holder.userPoint.setText("0 Puan");
                        }

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

            }

            @NonNull
            @Override
            public ScorBoard.ScorViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.scorboard_model_layout,viewGroup,false);
                ScorBoard.ScorViewHolder viewHolder = new ScorBoard.ScorViewHolder(view);
                return  viewHolder;
            }
        };

        adapter.startListening();
        scorrecy.setAdapter(adapter);

    }

    public static class ScorViewHolder extends RecyclerView.ViewHolder{
        TextView  userName , userPoint;
        View mView;

        public ScorViewHolder(@NonNull View itemView) {
            super(itemView);
            mView= itemView;

            userName =itemView.findViewById(R.id.rank_username);
            userPoint =itemView.findViewById(R.id.point);
        }
    }

ScorModel;评分模型;

  public String scor, username , currentUserID ;

    public ScorModel(){

    }

    public ScorModel(String scor,String username , String currentUserID) {
        this.scor= scor;
        this.username = username;
        this.currentUserID = currentUserID;

    }

  public String getScor() {
        return scor;
    }

    public void setScor(String scor) {
        this.scor = scor;
    }


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

Myy DB Ratings;我的数据库评级;

enter image description here在此处输入图像描述

Update:更新:

RatingsRef = FirebaseDatabase.getInstance().getReference().child("Ratings");
      
 RatingsRef.child(currentUserID).child("Ratings").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                double sum =  0.0;

                try {
                    for (DataSnapshot ds: dataSnapshot.getChildren()) {
                        Map<String,Object> map = (Map<String, Object>) ds.getValue();
                        Object rating =  map.get("rating");
                        Double pvalue = Double.parseDouble(String.valueOf(rating));
                        sum += pvalue;



                        Map userpoint = new HashMap();
                        userpoint .put("puan", String.valueOf(sum));

                        RatingsRef.child(currentUserID).updateChildren(userpoint );


                    }
                }catch (Exception e){

                }


            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException(); // don't ignore errors
            }
        });

You cannot get the desired results, because your puan field holds a value of type string and not a number.您无法获得所需的结果,因为您的puan字段包含字符串类型的值而不是数字。 One thing to remember is that when you order strings the results are ordered lexicographically .要记住的一件事是,当您对字符串排序时,结果是按字典顺序排序的。 For example, 10 is placed before 2. See an example below:例如,10 放在 2 之前。请参见下面的示例:

1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3

So the best option that you have is to change the type of the field to be number and not string.因此,您最好的选择是将字段的类型更改为数字而不是字符串。

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

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