Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

firebase - FirebaseRecyclerAdapter - populateViewHolder is not populating the data for first time it runs?

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

Here's my MainActivity

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);

And the 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

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...