0

I'm trying to show post contents sorted by time in an endless recyclerview.

So first I initialized the recyclerview

  FirebaseDatabase.getInstance().getReference().child("topics").child("all")
            .limitToLast(10)
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot d : dataSnapshot.getChildren()){

                        topics.add(d);
                           if (dataSnapshot.getChildrenCount()==topics.size()){

                            currentID = topics.get(0).getKey();
                            Collections.reverse(topics);
                            adapter.updateList(topics);


                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

Then on scroll end I called again

private void loadMoreData(){
    loading.setVisibility(View.VISIBLE);
    EACH_LOAD_DATA_NUMBER = EACH_LOAD_DATA_NUMBER + 10;
    FirebaseDatabase.getInstance().getReference().child("topics").child("all")
            .orderByChild("time")
            .limitToLast(EACH_LOAD_DATA_NUMBER)
            .endAt(currentID)
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot d : dataSnapshot.getChildren()){
                        topics.add(d);
                        if (dataSnapshot.getChildrenCount()==topics.size()){
                            currentID = topics.get(0).getKey();
                            Collections.reverse(topics);
                            adapter.updateList(topics);
                            Log.i("Current ID",topics.get(0).getKey());
                            loading.setVisibility(View.GONE);
                        }
                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

}

On First initialization stage it shows posts sorted by time and in reverse order on recyclerview.

And when I load more. It works fine for next 10 data. But then it repeats old data.

So where can be the problem :(

Thank you..

Bucky
  • 1,116
  • 2
  • 18
  • 34
  • Firebase pagination already has many useful questions here on stackoverflow. Please check [this one](https://stackoverflow.com/q/37711220/5861618) or search for [Firebase Pagination](https://stackoverflow.com/search?q=firebase+pagination) – Rosário Pereira Fernandes Jan 18 '18 at 18:06
  • Consider accepting the answer as it is the only available answer to your problem – sziraqui Feb 09 '18 at 07:50

1 Answers1

0

ValueEventListener will give you all the data at specified node. It's your job to avoid duplicates. You need to check if the data you received is already present in the list by calling list.contains(d). However for contains() method to work properly, you must override equals() and hashCode() in your Topic class.

Here's an example of overriding both (class name "Event"):

 @Override
    public boolean equals(Object o){
        if(this == o)
            return true;
        if(!( o instanceof Event))
            return false;

            Event f = (Event) o;
            if(this.eventid == f.getEventid())
                return true;
        return false;
    }

    @Override
    public int hashCode() {
        return eventid;
    }

Once you have properly overrided both methods you can easily avoid duplicates with

if(!topics.contains(d))
    topics.add(d)
sziraqui
  • 5,763
  • 3
  • 28
  • 37