I'm trying to do a pagination in firebase. I've followed this approach (Firebase pagination limitToFirst() vs limitToLast()) but I can't get a correct pagination items from firebase, always get the same 3 items.
This is the problem with the code below:
I have 12 items in my firebase database. I retrieve the 3 elements with the first load items, and when I try to load more items, I always get the same elements that I got in the first load.
I've tried with:
postList.size()-1).getTimestamp().toString()
and
postList.size()-3).getTimestamp().toString()
but always get the same data
private ArrayList<Post> postList = new ArrayList<>();
private Query allPostsQuery;
private final static int PAGESIZE = 3;
//First load items when start
linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);
mAdapter = new WallAdapter(postList, getFragmentManager(), getActivity(), navigation, mListener, WallFragmentOwn.this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(dy > 0) {
visibleItemCount = linearLayoutManager.getChildCount();
totalItemCount = linearLayoutManager.getItemCount();
pastVisiblesItems = linearLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
loading = false;
loadMoreItems();
}
}
}
}
});
//Load more
public void loadMoreItems(){
allPostsQuery = FirebaseUtil.getPostsRef().orderByChild("timestamp")
.endAt(postList.get(postList.size()-1).getTimestamp().toString())
.limitToLast(PAGESIZE);
allPostsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue() == null){
return;
}
dataSnapshot.getChildrenCount();
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
Post post = postSnapshot.getValue(Post.class);
post.setUid(postSnapshot.getKey());
postList.add(post);
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
UPDATE 1 I understand that I need to do the .endAt() with the same type that I have in Firebase database. This is my code know:
//POJO CLASS
private HashMap<String, Object> timestampCreated;
public Post(String text, HashMap<String, Object> timestampCreatedObj) {
this.text = text;
timestampCreatedObj = new HashMap<String, Object>();
timestampCreatedObj.put("date", ServerValue.TIMESTAMP);
this.timestampCreated = timestampCreatedObj;
}
@Exclude
public long getCreatedTimestampLong(){
return (long)timestamp;
}
My Query:
allPostsQuery = getBaseRef().child("posts").orderByChild("timestamp")
.endAt(postList.get(postList.size()-1).getTimestampCreatedLong())
.limitToFirst(PAGESIZE);
I Set the timestamp in Firebase in this way:
HashMap<String, Object> timestamp = new HashMap<String, Object>();
timestamp.put("timestamp", ServerValue.TIMESTAMP);
Post newPost = new Post(postText, timestamp);
postsRef.push().setValue(newPost);
JSON FIREBASE
{
"-Kt8GeVfyiebF8XhUUkL" : {
"text" : "0",
"timestampCreated" : {
"date" : 1504467921811
}
}
}
UPDATE 2
Change the tag inside orderByChild and know, is working pagination.
orderByChild("timestampCreated/date")