您好,我想按升序对用户进行排序,首先这是我的树:
java - 如何对数据升序排序Firebase数据库-LMLPHP
我想按我尝试使用categries.orderByChild("scoreTotal").orderByValue()的scoreTotal对用户进行排序,但这不起作用,这是我的代码:

public class Scores extends  Fragment {

    RecyclerView userlist;
    int i = 0;
    RecyclerView.LayoutManager layoutManager;
    FirebaseRecyclerAdapter<User, usersViewHolder> adapter;
    FirebaseDatabase database;
    ProgressBar loading;
    DatabaseReference categries, mDatabase;

    public Scores() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        categries = FirebaseDatabase.getInstance().getReferenceFromUrl("https://quizapp.firebaseio.com/Quizy/users");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_scores, container, false);
        userlist = v.findViewById(R.id.users);
        loading = v.findViewById(R.id.progress);
        layoutManager = new GridLayoutManager(getActivity(), 1);
        userlist.setLayoutManager(layoutManager);
        userlist.setHasFixedSize(true);
        mDatabase = FirebaseDatabase.getInstance().getReference();
        loadCategories();
        return v;
    }

    private void loadCategories() {

        adapter = new FirebaseRecyclerAdapter<User, usersViewHolder>(User.class, R.layout.useitem, usersViewHolder.class, categries.orderByChild("scoreTotal").orderByValue()) {
            @SuppressLint("CheckResult")
            @Override
            protected void populateViewHolder(final usersViewHolder viewHolder, final User model, int position) {
                viewHolder.getUserName().setText(model.getUsername());
                viewHolder.getUserScore().setText("Score : "+model.getScoreTotal());
                RequestOptions requestOptions = new RequestOptions();
                requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL).dontTransform();
                Glide.with(getActivity()).load(model.getImageBase64()).apply(requestOptions).listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        viewHolder.getProgressBar().setVisibility(View.GONE);
                        viewHolder.getUserpic().setVisibility(View.VISIBLE);
                        return false;

                    }
                }).into(viewHolder.getUserpic());
            }


            @Override
            public usersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                usersViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
                viewHolder.setItemOnClick(new itemOnClick() {
                    @Override
                    public void onClick(View v, int pos, boolean isLong) {

                    }
                });
                return viewHolder;
            }
        };
        adapter.notifyDataSetChanged();
        userlist.setAdapter(adapter);

    }
}

最佳答案

scoreTotal是数据库中的字符串。

如果您将值上传为数字(通常是整数),则该值周围将不包含引号。

最快的解决方案是重新评估您的上传代码以发送int。我强烈建议使用此方法,因为它是迄今为止最快的方法。

附带说明,您将需要在数据库规则中添加“ .indexOn”:[“ scoreTotal”]。

此外,如有必要,您可以在上传之前使用cloud函数解析字符串(例如,出于某种原因,无法直接上传int)。

您也可以在客户端上进行实用的语法分析和排序,但是由于这是最苛刻的解决方案,因此我不再赘述。

09-12 18:09