在我的应用程序中有一个积分系统。我想在零件中创建一个有序的点列表。考试将按照从高分到低分的顺序进行。在我写的代码中,这个系统不能正常工作。没有从高到低的排名。新评分的用户出现在顶部。我该如何解决这个问题?我想要它从最高分到最低值排序。
记分板类:
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;
}
my DB Ratings;
输入图片描述
更新:
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
}
});
您无法获得所需的结果,因为您的puan
字段保存的值类型为string并且而不是一个数字。要记住的一件事是,当您对字符串排序时,结果是按字典顺序排序的。例如,10放在2的前面。请看下面的例子:
所以最好的选择是将字段的类型更改为number和而不是字符串。1、10、11、12、13、14、15、16、17、18日,19日,2,20日,21日3