在Firebase中对数据进行排序 [英] Sorting Data in Firebase

查看:54
本文介绍了在Firebase中对数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase listview,如下所示,它的工作方式像一个超级按钮,但问题是它在listview(lv)的末尾显示了child("Posts")中的最后一个按键,我想要最后一个首先显示要按下的键,或者如果我可以按日期对其进行排序.

I'm using Firebase listview at its shown below and its working like a charm but the problem is it displays the last pushed key in the child("Posts") at the end of my listview(lv) I want the last pushed key to be displayed above all or if i can sort it by date.

    query = ref.child("Posts").orderByKey();
    FirebaseListAdapter<ChatRoomJ> adapter = new FirebaseListAdapter<ChatRoomJ>(
            ChatRoom.this,
            ChatRoomJ.class,
            R.layout.lvchatroom,query
    ) {
        @Override
        protected void populateView(View v, ChatRoomJ model, final int position) {

            final String date = model.getDate();
            final String displayName = model.getDisplayName();

            ((TextView) v.findViewById(R.id.displayname)).setText(displayName);
            ((TextView) v.findViewById(R.id.datel)).setText(date);


            });

        }
    };
    lv.setAdapter(adapter);

推荐答案

方法1:

除非您创建自己的扩展FirebaseListAdapter并覆盖getItem()方法的类,否则无法用当前代码对数据进行重新排序.这是一种很棒且简便的方法(由Monet_z_Polski提供).到目前为止,我使用了相同的技巧并获得了完美的结果.

There is no way to reorder the data with your current code, unless you make your own class that extends FirebaseListAdapter and override the getItem() method. This is a great and easy way to do it (courtesy of Monet_z_Polski). I used the same trick with perfect results so far.

public abstract class ReverseFirebaseListAdapter<T> extends FirebaseListAdapter<T>  {

public ReverseFirebaseListAdapter(Activity activity, Class<T> modelClass, int modelLayout, Query ref) {
    super(activity, modelClass, modelLayout, ref);
}

public ReverseFirebaseListAdapter(Activity activity, Class<T> modelClass, int modelLayout, DatabaseReference ref) {
    super(activity, modelClass, modelLayout, ref);
}

@Override
public T getItem(int position) {
  return super.getItem(getCount() - (position + 1));
}

然后,在制作适配器时,将使用新的自定义类,该类将修改发送到列表的每个项目的起始位置.

Then when you make the adapter, you use your new custom class that modifies the starting position for each item that is sent to the list.

方法2:

另一种方法是将数据读入您的应用程序(可能读入ArrayList或类似的东西)并在客户端上对数据重新排序,然后再使用正确排序的数据填充列表.这就像将数据读入ArrayList且每个项目的起始索引为0一样容易.例如chatRoomList.add(0, nextChatRoom);之类的东西.这样,当您在屏幕上填充列表时,它将按照您想要的顺序进行.

The other way would be to read the data in to your app (probably into an ArrayList or something like that) and reorder the data on the client before populating the list with the properly ordered data. This can be as easy as reading the data into the ArrayList with a start index of 0 for each item. Something like chatRoomList.add(0, nextChatRoom); for example. That way when you populate the list on the screen it will be in the order you want.

还有其他一些方法,包括制作自定义适配器,但我个人认为这比这两种方法要复杂.第一个是我的最爱.

There are other methods involving making a custom adapter, but I personally think that would be more complicated than these two methods. The first is my favorite.

这篇关于在Firebase中对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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