如何按降序使用orderByKey()而不是升序#Askfirebase

时间:2016-07-28 04:32:31

标签: android firebase firebase-realtime-database

大卫和Firebase团队, 我试图通过最近的" scout在FragmentStatePagerAdapter中订购标签。"将项目插入FragmentStatePagerAdapter并使其正确刷新似乎很难。因此,如果Firebase只按降序而不是提升(最近的第一个)给我按键,那么它将使我的生活更轻松。如果这不可能,请考虑这是orderByKeyDescending() :)的功能请求。任何帮助将不胜感激!谢谢!

这是我想要的听众BTW:

scoutRef.orderByKeyDescending().addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                mSectionsPagerAdapter.mKeyPosition.add(dataSnapshot.getKey());
                mSectionsPagerAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                FirebaseCrash.report(databaseError.toException());
            }
        });

6 个答案:

答案 0 :(得分:0)

Firebase Query用于排序。

Sort by key

Sort by child

Sort by value

Sort by priority

<强>更新

无法直接从Firebase按降序对值进行排序。

但是,您可以创建自定义布局并以相反的顺序创建视图。

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

source(They have used a RecyclerView)

答案 1 :(得分:0)

将每个孩子添加到列表顶部,而不是将其添加到结尾。

scoutRef.orderByKeyDescending().addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            mSectionsPagerAdapter.mKeyPosition.add(0, dataSnapshot.getKey());
            mSectionsPagerAdapter.notifyDataSetChanged();
        }

        ...
    });

答案 2 :(得分:0)

我找到了一种合理的方法,但是当一个新项目被添加到数据库并且ViewPager仍然处于活动状态时它不能正常工作(与另一个问题相同,它不刷新而你得到重复。请参阅我的评论:How to use orderByKey() in descending order instead of ascending #Askfirebase)。

这是我的解决方案:

final List<String> tmpKeyList = new ArrayList<>();
scoutReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                tmpKeyList.clear();

                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    if (tmpKeyList.size() < dataSnapshot.getChildrenCount() - 1) {
                        tmpKeyList.add(0, child.getKey());
                    } else {
                        tmpKeyList.add(0, child.getKey());
                        mSectionsPagerAdapter.setKeyPositionList(tmpKeyList);
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                FirebaseCrash.report(databaseError.toException());
            }
        });

我的FragmentStatePagerAdapter:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        List<String> mKeyPosition = new ArrayList<>();

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(mKeyPosition.get(position));
        }

        @Override
        public int getCount() {
            return mKeyPosition.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "SCOUT " + (getCount() - position);
        }

        public void setKeyPositionList(List<String> list) {
            mKeyPosition = new ArrayList<>(list);
            notifyDataSetChanged();
        }
    }

答案 3 :(得分:0)

非常感谢wisemann !!!

TL; DR您无法使用Firebase,期间按降序获取数据。你必须在客户端这样做。

我在客户端的解决方案是return POSITION_NONE (-2)中的getItemPosition。如果您想要更高效,可以在此处阅读有关标签的更多信息:ViewPager PagerAdapter not updating the View

这是完整的适配器:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    List<String> mKeyPosition = new ArrayList<>();

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(mKeyPosition.get(position));
    }

    @Override
    public int getCount() {
        return mKeyPosition.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "SCOUT " + (getCount() - position);
    }

    public void add(String key) {
        mKeyPosition.add(0, key);
        notifyDataSetChanged();
    }
}

Firebase监听器:

public void updateScouts(final CurrentScoutActivity.SectionsPagerAdapter mSectionsPagerAdapter) {
    scoutReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            mSectionsPagerAdapter.add(dataSnapshot.getKey());
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
           //...
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
           //...
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
           //...
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            FirebaseCrash.report(databaseError.toException());
        }
    });
}

答案 4 :(得分:0)

我无法从Firebase中以降序获取数据,而是颠倒了回收台视图的布局!希望对别人有帮助。

//reversing recycler view to get recent data at the top
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
    mLayoutManager.setReverseLayout(true);
    mLayoutManager.setStackFromEnd(true);
    // Set the layout manager to your recyclerview
    recyclerView.setLayoutManager(mLayoutManager);

答案 5 :(得分:0)

最新答案却很简单

scoutRef.orderByKey().addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                mSectionsPagerAdapter.insert(dataSnapshot.getKey(), 0);
                mSectionsPagerAdapter.notifyDataSetChanged();
            }