I am trying to paginate in firebase. I followed this approach ( Limit pagination for FirebaseToFirst () vs limitToLast () ), but I cannot get the correct pagination elements from firebase, I always get the same 3 elements.
This is a problem with the code below:
I have 12 items in a firebase database. I retrieve 3 elements with the first elements to load, and when I try to load more elements, I always get the same elements that I received when I first loaded.
I tried: postList.size () -. 1) .getTimestamp () ToString ()
as well as 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 the items at startup
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(); } } } } });
// Download 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 .endAt () with the same type that I have in the Firebase database. This my code knows:
// 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 request:
allPostsQuery = getBaseRef().child("posts").orderByChild("timestamp") .endAt(postList.get(postList.size()-1).getTimestampCreatedLong()) .limitToFirst(PAGESIZE);
I Set the timestamp in Firebase as follows:
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 you know it works with pagination.
orderByChild("timestampCreated/date")