1

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:

nodes

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

1 Answers1

0

I will try to give you the idea, the below is not tested. But it should give you the last comment on first load and then every time you load more it should give you one by one the next comments:

Try to keep a reference of the time of the comment before your last comment:

//have this as a member field to keep a reference of it

long dateBeforeLastComment = 0;

//have this also as member field (don't recreate this or you will be clearing it every time you load more comments

commentList = new ArrayList<>();

The first time you load, load the last comment and keep reference for the time before it:

private void loadData() {

query =Util.mServerDatabaseRef.child(Util.getServer().getServerUID()).child("timeline").child("commentList").orderByChild("date").limitToLast(2);


query.addListenerForSingleValueEvent(new ValueEventListener() {

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

int index = 0;

for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
index++;

if(index == 1){

//keep reference
dateBeforeLastComment = userSnapshot.child("date").getValue(Long.class);

}else{
//add to list
commentList.add(userSnapshot.getValue(Comment.class));
adapter.notifyDataSetChanged();
}

}

mIsLoading = false;

}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
mIsLoading = false;
}




}

Now every time you want to load more try to end at the reference of the date before last:

private void loadMoreData() {

if(dateBeforeLastComment != 0){

query =Util.mServerDatabaseRef.child(Util.getServer().getServerUID()).child("timeline").child("commentList").orderByChild("date").endAt(dateBeforeLastComment).limitToLast(2);


query.addListenerForSingleValueEvent(new ValueEventListener() {

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


int index = 0;

for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
index++;

if(index == 1){

//keep reference again
dateBeforeLastComment = userSnapshot.child("date").getValue(Long.class);

}else{

//add to list
commentList.add(userSnapshot.getValue(Comment.class));
adapter.notifyDataSetChanged();
}

}

mIsLoading = false;

}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
mIsLoading = false;
}


}

}
Hasan Bou Taam
  • 3,748
  • 1
  • 7
  • 19