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

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

问题描述

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

例如:如果我想要最贵的东西,可以将其放在listView的顶部,将最便宜的放在底部.

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

这就是我的计数:

  @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数据库查询的结果进行排序,则需要使用数字作为值,而使用字符串.如果您保留字符串,如我在屏幕快照中所看到的,则需要知道字符串是按lexicographically排序的,并且我确定这不是您要查找的.

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

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

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

此方法将使您的订单撤消.

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

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

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.

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.

This is what I have for my count:

  @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) {

            }
        });
    }

解决方案

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.

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.

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.

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

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天全站免登陆