I have a feed filled by comments and I simply it to load the newest ones first and when the user request more comments they will get loaded (from the newest to the oldest one).
This is how my db looks like:
So let's suppose I want to return only 1 comment at a time for starting... The feed would start with the last comment in the db list cause it's the more recent one and then when the user request it I want to load the middle one but it has to be placed below on my recyclerView and so on...
Here's my code:
private void loadData(long lastDate) {
Query query;
if (lastDate == 0) {
query = Util.mServerDatabaseRef.child(Util.getServer().getServerUID()).child("timeline").child("commentList").orderByChild("date").limitToLast(1);
} else {
query = Util.mServerDatabaseRef.child(Util.getServer().getServerUID()).child("timeline").child("commentList").orderByChild("date").endAt(lastDate).limitToFirst(1);
}
new Thread(() -> query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
commentList = new ArrayList<>();
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
commentList.add(userSnapshot.getValue(Comment.class));
adapter.notifyDataSetChanged();
}
mIsLoading = false;
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
mIsLoading = false;
}
})).start();
}
lastDate is the date of the last comment is my adapter list;
The first loading works just fine, but I can't fix this to retrieve the comments from newest to oldest and place it below each time. The way it is it will return or the same comment or the oldest one and will place it on the top of the list. Of course I need it to work for more than 1 comment at a time too. Any help is appreciated