如何按照从第一个到最早的时间顺序对父项进行排序,例如instagram Firebase

时间:2017-04-07 20:06:26

标签: android firebase firebase-realtime-database

Hello stackoverflow我有一个问题,这个订购我里面的物品" post"这是一个父母"我需要父母内部的物品 push方法生成的id按时间顺序排序

尝试使用此方法:.orderByKey(),我已经阅读了文档并告诉我使用该方法但它不起作用

这是我在照片中的数据库示例,它很好地解释了我需要订购的内容 My database photo

这些是我使用的方法和参考:

适配器方法:

  private void setupAdapter() {
    mPostAdapter = new FirebaseRecyclerAdapter<Post, PostHolder>(
            Post.class,
            R.layout.row_post,
            PostHolder.class,
            FirebaseUtils.getPostRef().orderByKey()
    ) {
        @Override
        protected void populateViewHolder(final PostHolder viewHolder, final Post model, int position) {
            Post mPost = new Post();

            viewHolder.setNumComments(String.valueOf(model.getNumComments()));
            viewHolder.setNumLikes(String.valueOf(model.getNumLikes()));
            viewHolder.setNumDislikes(String.valueOf(model.getNumDislikes()));
            viewHolder.setTime(DateUtils.getRelativeTimeSpanString(model.getTimeCreated()));
            viewHolder.postOwnerUsernameTextView.setText(model.getUsername());
            if(viewHolder.postOwnerUsernameTextView == null){
                viewHolder.postOwnerUsernameTextView.setText("Anonymous");
            }
            viewHolder.setPostText(model.getPostText());


                final  StorageReference storageReference = FirebaseStorage.getInstance()
                        .getReference(model.getPostImageUrl());


                Glide.with(getActivity())
                        .using(new FirebaseImageLoader())
                        .load(storageReference).into(viewHolder.postDisplayImageView); ... remainder of code

我试过这个

  mPostAdapter = new FirebaseRecyclerAdapter<Post, PostHolder>(
        Post.class,
        R.layout.row_post,
        PostHolder.class,
        FirebaseUtils.getPostRef().getParent().orderByKey 

但这样做不会在populate viewHolder

中收到要在适配器中呈现的数据

utils:

 public static DatabaseReference getPostRef() {
     return FirebaseDatabase.getInstance()
            .getReference("posts");
  }
  public static Query getPostQuery() {
    return getPostRef().orderByChild("timeCreated");
 }
 public static DatabaseReference getMyPostRef() {
     return FirebaseDatabase.getInstance().getReference("myPosts")
             .child(getCurrentUser().getEmail().replace(".", ","));

1 个答案:

答案 0 :(得分:0)

找到一个解决方案,但很难具有讽刺意味的是它非常容易

像这样设置LinearLayoutManager

 mPostRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recyclerview_post);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);
    mPostRecyclerView.setLayoutManager(linearLayoutManager);
    setupAdapter();
    mPostRecyclerView.setAdapter(mPostAdapter);

这都是