FirebaseRecyclerAdapter - populateViewHolder是不是第一次运行它的数据填充? [英] FirebaseRecyclerAdapter - populateViewHolder is not populating the data for first time it runs?
问题描述
populateViewHolder
第一次运行时没有填充数据,但是当我关闭应用程序,打开它,数据绑定在 RecyclerView
查看持有人。
我不明白为什么数据不填充第一次,它显示空白屏幕
这是我的 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);
$ b //使用Query
设置FirebaseRecyclerAdapter //查询postsQuery = mDatabase.child(EN)。child(Courses );
DatabaseReference ref = FirebaseDatabase.getInstance()。getReference()。child(EN)。child(Courses);
ref.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
Toast.makeText(MainActivity.this,dataSnapshot.toString(),Toast .LENGTH_SHORT).show();
}
$ b $ @覆盖
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(查看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());
$ b
My Model Pojo
public class Course {
public String description;
public String title;
$ b $ public Course(){
//调用DataSnapshot.getValue(Post.class)所需的默认构造函数
}
public Course(String标题,字符串描述){
// this.description = author;
this.title = title;
this.description = description;
}
public String getName(){
return title;
}
public String getDescription(){
return description;
$ div $解析方案
找到
$ b $问题是 RecyclerView
的高度 wrap_content
。所以请确保您的 RecyclerView
高度设置为 match_parent
。
< android.support.v7.widget.RecyclerView
android:id = @ + id / recycler_view
android:layout_width =match_parent
android:layout_height =match_parent
android:scrollbars =vertical/>
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;
}
}
Found the Solution.
The Problem is RecyclerView
had a height of wrap_content
. So Please make sure your RecyclerView
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" />
这篇关于FirebaseRecyclerAdapter - populateViewHolder是不是第一次运行它的数据填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!