在客户端对FirebaseRecyclerAdapter进行排序

时间:2017-09-05 14:02:15

标签: android firebase firebase-realtime-database

我想在TIME BASIS上对FirebaseRecyclerAdapter进行排序。我正在研究那个应用程序,所以每当新消息出现时,那个对话框就会出现在布局的顶部,它还包含发送消息的时间,所以消息是新的然后应该在回收器视图的顶部。那么那么如何在时间基础上对FirebaseRecyclerAdapter进行排序
这是我的代码

madapter = new FirebaseRecyclerAdapter<user, contact.UserViewHolder>(user.class, R.layout.activity_dialogs_list, contact.UserViewHolder.class, muserref) {
        @Override
        protected void populateViewHolder(final UserViewHolder viewHolder, user model, final int position) {
            final String ais = model.getName();
            viewHolder.setName(model.getName());
            final String b = model.groupId;
            //Toast.makeText(contact.this,viewHolder.getLayoutPosition(),LENGTH_SHORT).show();
            mref = FirebaseDatabase.getInstance().getReference().child("LastMessage").child(b);
            mref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot childSnapShot : dataSnapshot.getChildren()) {
                        val=lmlm;
                        HashMap<String, Object> hashmap = (HashMap) dataSnapshot.getValue();
                        val = (String) hashmap.get("lastm");
                        Long role = (Long) hashmap.get("timestampCreated");
                        String lastsender = (String) hashmap.get("LastmessageSender");
                        getTimeago getTimeAgo = new getTimeago();
                        String lastSeenTime = getTimeAgo.getTimeAgo(role);
                        //SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
                        //String dateAsString = sdf.format (role);
                        //change(position);
                        //   swape(viewHolder.getAdapterPosition());

                        if (val.length() > 40) {
                            String nawa = val.substring(0, 40);
                            viewHolder.setLastmessage((nawa + "..."));
                            viewHolder.setLastTime(lastSeenTime);

                        } else {
                            viewHolder.setLastmessage(val);
                            viewHolder.setLastTime(lastSeenTime);

                        }
                        //viewHolder.setLastmessage(val);
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            mimage = FirebaseDatabase.getInstance().getReference().child("Image").child(b);
            mimage.keepSynced(true);
            mimage.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot childSnapShot : dataSnapshot.getChildren()) {

                        HashMap<String, Object> hashmap = (HashMap) dataSnapshot.getValue();
                        final String vala = (String) hashmap.get("image");

                        viewHolder.setImage(vala, getApplicationContext());
                        /// //viewHolder.setImage(val,getApplicationContext());
                        //viewHolder.setImage(vala);
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Toast.makeText(contact.this,"Something Went wrong",LENGTH_SHORT).show();
                }
            });




                }
            });
        }

    };
    mUserlist.setAdapter(madapter);
    madapter.notifyDataSetChanged();

这是我的持有人    ` public static class UserViewHolder扩展了RecyclerView.ViewHolder {

    View mview;
    ImageLoader imageLoader;

    public UserViewHolder(View itemView) {
        super(itemView);
        mview = itemView;

    }

    public void setName(String name) {
        TextView username = (TextView) mview.findViewById(R.id.dialogName);
        username.setText(name);
    }

    public void setLastmessage(String lastmessage) {

        TextView set = (TextView) mview.findViewById(R.id.dialogLastMessage);
        set.setText(lastmessage);


    }

    public void setLastTime(String lastTime) {
        TextView time = (TextView) mview.findViewById(R.id.dialogDate);
        time.setText(lastTime);
    }


    public void setImage(final String vala, final Context applicationContext) {
        final CircleImageView cm = (CircleImageView) mview.findViewById(R.id.dialogAvatar);
        Picasso.with(applicationContext).load(vala).networkPolicy(NetworkPolicy.OFFLINE).into(cm, new Callback() {
            @Override
            public void onSuccess() {
                Picasso.with(applicationContext).load(vala).into(cm);
            }

            @Override
            public void onError() {
                Picasso.with(applicationContext).load(vala).into(cm);
            }
        });


    }

`

So if new Message comes in SE Comps then that Dialog Show goes on The Top of RecyclerView 那么我该怎样才能得到帮助

1 个答案:

答案 0 :(得分:0)

您是否尝试使用orderByChild函数对firebase列表数据进行排序?

ref.orderByChild(&#39;&lt; time_key&gt;&#39;)

OrderByChild将与ChildEventListner

一起使用

check this link