0

I have a problem using FirebaseRecyclerAdapater, at first it was working fine but now this adapter is firing twice. The database reference is only referring one child, but it is always firing twice. The Toast with text "counter" will appear twice

FirebaseRecyclerAdapter<RequestVisit, RequestViewHolder> requestAdapter = 
    new FirebaseRecyclerAdapter<RequestVisit, RequestViewHolder>(
    RequestVisit.class,
    R.layout.seekerrequests_layout,
    RequestViewHolder.class,
    requestDatabase.child("2DBwmhGplGMoAlLy6337HZEShi93")
) {
@Override
protected void populateViewHolder(final RequestViewHolder viewHolder, RequestVisit model, int position) {
     Toast.makeText(getContext(), "counter" + 
     viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
    }
};
requestVisitList.setAdapter(requestAdapter);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Maybe you are using `addValueEventListener`, try using `addListenerForSingleValueEvent` this will only get your data from Firebase once and will not listen for updates. – Ahmed Abidi May 12 '18 at 09:57
  • @AhmedAbidi The `FirebaseRecyclerAdapter` handles all the listeners in this case. It intentionally doesn't use `addListenerForSingleValueEvent`, since it keeps the recycler view in sync with changes in the database. – Frank van Puffelen May 12 '18 at 14:41
  • @CjZayin: If `populateViewHolder` gets called with two different positions, that means there are two children under `requestDatabase.child("2DBwmhGplGMoAlLy6337HZEShi93")`. If that doesn't explain it, can you edit your question to include the data under `2DBwmhGplGMoAlLy6337HZEShi93`? You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen May 12 '18 at 14:45
  • @FrankvanPuffelen yes, there are two children under this `requestDatabase.child("2DBwmhGplGMoAlLy6337HZEShi93")`. Thank you for pointing this out – Cj Zayin Queenie Cabug-os May 12 '18 at 23:38

1 Answers1

0

A Firebase*Adapter shows a list of items, the child nodes under the location that you attach it to.

If populateViewHolder gets called with two different positions, that means there are two children under requestDatabase.child("2DBwmhGplGMoAlLy6337HZEShi93").

Keep in mind that if 2DBwmhGplGMoAlLy6337HZEShi93 is a child node with two properties, then your approach will call populateViewHolder for each of those properties.

If you want to show only a single item in the RecyclerView, you can create a simple query with:

FirebaseRecyclerAdapter<RequestVisit, RequestViewHolder> requestAdapter = 
    new FirebaseRecyclerAdapter<RequestVisit, RequestViewHolder>(
    RequestVisit.class,
    R.layout.seekerrequests_layout,
    RequestViewHolder.class,
    requestDatabase.orderByKey().equalTo("2DBwmhGplGMoAlLy6337HZEShi93")
) 
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807