question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

populateViewHolder() isn't getting executed in a FirebaseRecyclerAdapter for the first time when app runs

See original GitHub issue

FirebaseRecyclerAdapter - populateViewHolder is not populating the data for the first time it runs but when I closed the app and opened it, the data is binded in RecyclerView View Holder. I am not getting why data is not populating for first time, it is showing blank screen

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

Main Activity

mDatabase = FirebaseDatabase.getInstance().getReference();
        // [END create_database_reference]

        mRecycler = (RecyclerView) findViewById(R.id.recycler_view);
        mRecycler.setHasFixedSize(true);
        setSupportActionBar(toolbar);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);
        mRecycler.setLayoutManager(layoutManager);




        // Set up FirebaseRecyclerAdapter with the Query
        //Query postsQuery = mDatabase.child("EN").child("Courses") ;
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("EN").child("Courses");
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Toast.makeText(MainActivity.this, dataSnapshot.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(MainActivity.this, databaseError.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        Log.d("ref", String.valueOf(ref));
        //   Log.d("Query", String.valueOf(postsQuery));
        mAdapter = new FirebaseRecyclerAdapter<Course, CourseViewHolder>(Course.class, R.layout.item_home,
                CourseViewHolder.class, ref) {
            @Override
            protected void populateViewHolder(final CourseViewHolder viewHolder, final Course model, final int position) {
                viewHolder.bindToPost(model);
            }

        };
        mRecycler.setAdapter(mAdapter);`

ViewHolder

`public class CourseViewHolder extends RecyclerView.ViewHolder {

public TextView titleView;
public TextView descriptionView;
public ImageView IconView;
public TextView lessonCountView;


public CourseViewHolder(View itemView) {
    super(itemView);

    titleView = (TextView) itemView.findViewById(R.id.title);
    descriptionView = (TextView) itemView.findViewById(R.id.description);
    //IconView = (ImageView) itemView.findViewById(R.id.star);

}

public void bindToPost(Course post) {
  //  Log.d("Post", String.valueOf(post));
    titleView.setText(post.getName());
    descriptionView.setText(post.getDescription());

}
}`

My Model Pojo

@IgnoreExtraProperties
   public class Course {

public String description;
public String title;


public Course() {
    // Default constructor required for calls to DataSnapshot.getValue(Post.class)
}

public Course(String title, String description) {
  //  this.description = author;
    this.title = title;
    this.description =description;

}

public String getName() {
    return title;
}


public String getDescription() {
    return description;
}



 }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
prashanth726commented, Sep 5, 2016

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"
            />
1reaction
filipebezerracommented, Sep 5, 2016

The same bug happened with me. I have the same view logic as @itsPR. Was solved using his solution, applying android:layout_height=“match_parent” to my RecyclerView.

So, in my oppinion this is a bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

populateViewHolder is not populating the data for first time it ...
FirebaseRecyclerAdapter - populateViewHolder is not populating the data for the first time it runs but when I closed the app and opened it, ......
Read more >
firebase recycler adapter tutorial || On Populate ... - YouTube
In this video i am showing how to bind view in FirebaseRecyclerAdapter Using Exoplayer in recyclerview Watch it's first part ...
Read more >
Firebase Realtime Database By Example with Android - Medium
In this blog post, I present a complete, step by step tutorial on working with Firebase Realtime Database in Android.
Read more >
Updating Data After Gesture Interactions
Once deleted, the FirebaseRecyclerAdapter will automatically update the view. Now if we run our app and drag an item, the adjacent items will...
Read more >
Firebase & Android, a Real-Time Match Synced in Heaven
Firebase provides a real-time database & handles authentication, among other features. It allows developers to not worry about the backend.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found