如何按升序或降序排列 Firebase 数据库数据? [英] How to arrange firebase database data in ascending or descending order?

查看:30
本文介绍了如何按升序或降序排列 Firebase 数据库数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个 firebase 数据库,我想知道如何制作它以便我的 listView 按升序或降序显示我的数据.

I have setup a firebase database and I was wondering how I'd make it so that my listView shows my data in ascending or descending order.

例如:如果我想要最贵的东西,我会将它放在 listView 的顶部,而将便宜的放在底部.

For example: if I wanted something thats the most expensive I'd have it at the top of my listView and the cheaper ones at the bottom.

基本上,我正在尝试创建一个排名活动,其中我有两个单独的列表视图,它们按成本排名并单独计数.

Basically I'm trying to create a ranking activity where I've two seperate listviews that rank by cost and count individually.

这就是我所拥有的:

  @Override
    protected void onStart() {
        super.onStart();

        itemlists=new ArrayList<String>();
        databaseItems.child("items").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot itemSnapShot : dataSnapshot.getChildren()) {
                    String itemname=itemSnapShot.child("itemName").getValue().toString();

                    itemlists.add(itemname);
                    adapter = new ArrayAdapter(SearchActivity.this,android.R.layout.simple_list_item_1,itemlists);
                    listFinalCount.setAdapter(adapter);

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

推荐答案

如果您想对 Firebase 数据库查询的结果进行排序,您需要使用数字作为值而不是 字符串.如果您保留字符串,如我在屏幕截图中所见,您需要知道字符串是按字典顺序 排序的,我确定这不是您要查找的内容.

If you want to order the results of a Firebase database query, you need to use numbers as values and not Strings. If you keep the Strings, as I see in your screenshot, you need to know that the Strings are ordered lexicographically and I'm sure this is not what you are looking for.

按升序或降序对数据进行排序的最简单方法是在客户端.默认情况下,Firebase 可以按给定属性按升序对结果进行排序.Firebase 中没有按降序排序的方法,但可以进行一些调整.

The simplest way to order data in ascending or descending order, would be on client side. Firebase can order the results in ascending order by a given property by default. There is no method in Firebase to order in descending order but there a few tweaks that can be made.

如果您使用 RecyclerView 来显示数据,最简单的方法是使用以下代码:

If you are using a RecyclerView to display data, the simplest way would be to use the following code:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);

这个方法会颠倒你的顺序.

This approch will reverse you the order.

另一种方法是创建您自己的适配器,扩展 FirebaseListAdapter 并覆盖 getItem() 方法.

Another approach would be to create your own adapter that extends FirebaseListAdapter and override getItem() method.

另一种方法是从数据库中获取数据,将其添加到CollectionList 将是一个很好的解决方案,然后使用Collections.reverse(yourList);.

Another approach would be to get the data from the database, add it to a Collection, a List would be a good solution and then use Collections.reverse(yourList);.

这篇关于如何按升序或降序排列 Firebase 数据库数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆