4

I have below structure in my firebase console . I am trying to read the values and display all the users but populateViewHolder never gets called.

   users
     +OW5BYennVRXvfzOjfKpup9rZEYv2
     -email: "abc@gmail.com"
     -username: "abc"

While in the below code I receive the item count as 1

 mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            int friendlyMessageCount = mAdapter.getItemCount();
            int lastVisiblePosition =
                    mManager.findLastCompletelyVisibleItemPosition();
            // If the recycler view is initially being loaded or the
            // user is at the bottom of the list, scroll to the bottom
            // of the list to show the newly added message.
            if (lastVisiblePosition == -1 ||
                    (positionStart >= (friendlyMessageCount - 1) &&
                            lastVisiblePosition == (positionStart - 1))) {
                recyclerView.scrollToPosition(positionStart);
            }
        }
    }); 

Here is my code

    @Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment_home_page, container, false);

    mDatabase = FirebaseDatabase.getInstance().getReference();

    recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);

    return rootView;
}
      @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mManager = new LinearLayoutManager(getActivity());
    mManager.setReverseLayout(true);
    mManager.setStackFromEnd(true);
    Query postsQuery = mDatabase.child("users");
    mAdapter = new FirebaseRecyclerAdapter<User, PostViewHolder>(User.class, R.layout.post,
            PostViewHolder.class, postsQuery) {

        @Override
        protected void populateViewHolder(PostViewHolder viewHolder, User model, int position) {
            final DatabaseReference postRef = getRef(position);
            final String postKey = postRef.getKey();
            viewHolder.bindToPost(model);
        }
    };

    mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            int friendlyMessageCount = mAdapter.getItemCount();
            int lastVisiblePosition =
                    mManager.findLastCompletelyVisibleItemPosition();
            // If the recycler view is initially being loaded or the
            // user is at the bottom of the list, scroll to the bottom
            // of the list to show the newly added message.
            if (lastVisiblePosition == -1 ||
                    (positionStart >= (friendlyMessageCount - 1) &&
                            lastVisiblePosition == (positionStart - 1))) {
                recyclerView.scrollToPosition(positionStart);
            }
        }
    });

    recyclerView.setAdapter(mAdapter);
}

PlaceHolder :

   public class PostViewHolder  extends RecyclerView.ViewHolder {
public TextView title, emailt;

public PostViewHolder(View view) {
    super(view);
    title = (TextView) view.findViewById(R.id.title);
    emailt = (TextView) view.findViewById(R.id.count);

}

public void bindToPost(User post, View.OnClickListener starClickListener) {
    title.setText(post.username);
    emailt.setText(post.email + "Likes");

}

public void bindToPost(User post) {
    title.setText(post.username);
    emailt.setText(post.email + "Likes");
}
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MRX
  • 1,400
  • 3
  • 12
  • 32
  • 1
    Strange but if I am running the sample database app its working fine. One more observation if I am debugging the app and wait at the debug point on Query for 2-3 seconds populateViewHolder is called and the view displays .. No idea why this is happening – MRX Aug 02 '16 at 11:57
  • I honestly have no idea why this is happening. It turns out I didn't solve my problem (which was this wasn't working in FRAGMETS) by simply not using a fragment. I'll like to find a solution too – Ali Bdeir Aug 03 '16 at 12:57
  • The best person for this is Frank van Puffelen – Ali Bdeir Aug 03 '16 at 12:58
  • you got it working @MRX Please help http://stackoverflow.com/questions/39268663/firebaserecycleradapter-populateviewholder-is-not-populating-the-data-for-firs – Prashanth Sep 01 '16 at 13:53

1 Answers1

1

The Problem is RecyclerView had a height of WrapContent , So Please Make Sure your recycler Height is set to Match_Parent. This will fix this issue.

<android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            />
Prashanth
  • 1,309
  • 2
  • 9
  • 17