1

I am trying to sort in the recyclerview the photos that are uploaded in order of time, but for some reason my code does not work.

The code of the adapter of the recyclerview:

 private void setupAdapter() {
    mPostAdapter = new FirebaseRecyclerAdapter<Post, PostHolder>(
            Post.class,
            R.layout.row_post,
            PostHolder.class,
            FirebaseUtils.getPostQuery()
    ) {

the code of the utils class:

 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("my_posts")
            .child(getCurrentUser().getEmail().replace(".", ","));
}

Here is the database structure:

{
  ...
  "my_posts" : {
    "a@aa,com" : {
      "-KgHOJHOGHAPGyBuVbF5" : true
    },
    "afaf@sg,com" : {
      "-KgHXFz3NoshQNKbXz53" : true,
      "-KgHZJFpkQ7lxx7HcxEV" : true
    },
    ...
  },
  "posts" : {
    "-KgHOJHOGHAPGyBuVbF5" : {
      "numComments" : 0,
      "numLikes" : 0,
      "postId" : "-KgHOJHOGHAPGyBuVbF5",
      "postImageUrl" : "post_images/image:47620",
      "postText" : "",
      "timeCreated" : 1490662278306
    },
    ...
  },
  "posts_liked" : {
    "dskato0603@gmail,com" : {
      "-KgWIDHRyVoX4kHQlCvk" : true
    }
  },
  "user_record" : {
    "a@aa,com" : {
      "posts" : [ "-KgHOJHOGHAPGyBuVbF5" ]
    },
    "jxjsjskss@kfks,com" : {
      "comments" : [ "-KgLRZCT7A_OIZXwa5RD", "-KgLRaAybYi9p1AnO5tN", "-KgMUAKOc30PBRwm5icX" ],
      "posts" : [ "-KgHiMD5jlUcnHcLWcAi", "-KgL3MrPw2D6-N6VACKF", "-KgLDQxqaxVQNs9WcdEW" ]
    }
    ...
  }
}
AL.
  • 36,815
  • 10
  • 142
  • 281
MADMVX
  • 354
  • 1
  • 4
  • 18
  • Please share the JSON tree that you're trying to display (as text, no screenshots please). You can easily get this by clicking the Export JSON link in [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just [a Good Thing to do](https://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557). – Frank van Puffelen Mar 31 '17 at 00:03
  • @FrankvanPuffelen the json archive that download in the init of the console of firebase? – MADMVX Mar 31 '17 at 00:05
  • No. The JSON that is in your database. Follow the first link in my comment, select your project and you'll be right in the database console. – Frank van Puffelen Mar 31 '17 at 00:06
  • is this? https://mega.nz/#!KtRFwKqA!pdxJHiDJLEgWaFuWmay6sIwn2DIy_CfwkB0asG-yHCM @FrankvanPuffelen – MADMVX Mar 31 '17 at 00:15
  • @Dskato I've edited your question included your database structure. Please edit it if I removed any data that's matter. And next time, please do it this way instead of uploading the whole database it to other site. – koceeng Mar 31 '17 at 01:01
  • @koceeng Thank you very much for your help But I still do not know how to solve that question – MADMVX Mar 31 '17 at 01:05
  • I don't see any problem from the code you post. So let me suggest some thing to check: (1) did you attach your adapter to `RecyclerView`? (2) did your `RecyclerView` has layoutmanager? (3) did your `RecyclerView` has `setHasFixedSize(true)`? (4) If none of that work, try debug your code inside `populateViewHolder` and see whats happening – koceeng Mar 31 '17 at 01:12
  • Everything is going well but the problem is that in the recycler I see photos photos and I want them to be sorted by time, so when I post a photo this is shown above in the beginning, because now the photos are shown in reverse from the most Old to new, and i put the order by child but doesnt works @koceeng – MADMVX Mar 31 '17 at 01:21
  • You should use "sort by order of time descending" not "sort does not work" as title. You can only do that on client side, means you have to get all the data first then sort it java/android style. [Check out this post](http://stackoverflow.com/a/34158197/4112725) – koceeng Mar 31 '17 at 01:27
  • 1
    @koceeng Sorry I'm new to this right now I change the title, and as I do thank you very much for your help I really appreciate it – MADMVX Mar 31 '17 at 01:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139541/discussion-between-dskato-and-koceeng). – MADMVX Mar 31 '17 at 01:57

1 Answers1

0

Try this:

LinearLayoutManager linearlayoutmanager = new LinearLayoutManager(context this);
 linearlayoutmanager.setReverselayout(true);
 linearlayoutmanager.setStackFromEnd(true);
RecyclerView.setLayoutManager(linearlayoutmanager);