如何对数据升序Firebase数据库进行排序

时间:2018-03-26 23:46:25

标签: java android firebase firebase-realtime-database

嗨我正在尝试按升序对用户进行排序,首先是我的树: enter image description here 我希望按 scoreTotal 对用户进行排序我尝试使用categries.orderByChild("scoreTotal").orderByValue(),但这里不起作用是我的代码:

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

    }
}

1 个答案:

答案 0 :(得分:1)

scoreTotal是数据库中的字符串。

如果您已将值上传为数字(通常为整数),则不会在其周围加上引号。

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

作为旁注,您需要在数据库规则中添加“.indexOn”:[“scoreTotal”]。

此外,如有必要,您可以使用云功能在上传之前解析sting(即由于某种原因无法直接上传int)。

您也可以在客户端进行实际解析和排序,但因为这是最棘手的解决方案,我不会去解释它。