Recycler View 不显示数据
Posted
技术标签:
【中文标题】Recycler View 不显示数据【英文标题】:Recycler View does not show data 【发布时间】:2021-08-28 07:49:02 【问题描述】:Recycler 视图不读取或显示来自 firebase 数据库的数据,这两天我想我想请你帮忙。 问题肯定不在 Firebase 数据库中,因为数据写入成功,但输出有问题。在我看来,问题出在 onStart,或者更确切地说是在将适配器连接到 Recycler 视图中
public class HomeActivity extends RootActivity
FirebaseAuth mAuth;
private FloatingActionButton fab;
private MaterialCardView PostCard;
private MaterialToolbar topAppBar;
private NavigationView nav_view;
private LottieAnimationView serach;
private TextView userName, userLogin;
private RecyclerView PostRV;
private DrawerLayout drawerLayout;
Context context;
PostAdapter adapter;
private ImageView userPick;
DatabaseReference db;
FirebaseDatabase firebaseDatabase;
FirebaseStorage fs;
StorageReference sr;
FirebaseUser currentUser;
List<Post> postList;
@Override
protected void onStart()
super.onStart();
db.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull @NotNull DataSnapshot snapshot)
postList = new ArrayList<>();
for (DataSnapshot postsnap : snapshot.getChildren())
Post post = postsnap.getValue(Post.class);
postList.add(post);
adapter = new PostAdapter(context, postList);
PostRV.setAdapter(adapter);
@Override
public void onCancelled(@NonNull @NotNull DatabaseError error)
);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
PostRV = (RecyclerView)
findViewById(R.id.PostRV);
nav_view = (NavigationView)
findViewById(R.id.nav_view);
drawerLayout = (DrawerLayout)
findViewById(R.id.drawerLayout);
fab = (FloatingActionButton)
findViewById(R.id.fab);
topAppBar = (MaterialToolbar)
findViewById(R.id.topAppBar);
PostRV.setLayoutManager(new
LinearLayoutManager(HomeActivity.this));
firebaseDatabase = FirebaseDatabase.getInstance();
db = firebaseDatabase.getReference("Posts");
topAppBar.setNavigationOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
drawerLayout.openDrawer(GravityCompat.START);
);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
FirebaseStorage fr = FirebaseStorage.getInstance();
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
View hl = nav_view.getHeaderView(0);
TextView userName = hl.findViewById(R.id.userName);
TextView userLogin = hl.findViewById(R.id.userLogin);
ImageView userPick = hl.findViewById(R.id.userPick);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(HomeActivity.this, AddActivity.class);
startActivity(intent);
);
后适配器
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.MyViewHolder>
Context mContext;
List<Post> mData;
public PostAdapter(Context mContext, List<Post> mData)
this.mContext = mContext;
this.mData = mData;
@NonNull
@NotNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType)
View row = LayoutInflater.from(mContext).inflate(R.layout.row_signle_post,parent,false);
return new MyViewHolder(row);
@Override
public void onBindViewHolder(@NonNull @NotNull MyViewHolder holder, int position)
holder.tvTitle.setText(mData.get(position).getTitle());
holder.tvTitle.setText(mData.get(position).getUsername());
Glide.with(mContext).load(mData.get(position).getUserPick()).into(holder.imgUserPick);
Glide.with(mContext).load(mData.get(position).getPostImage()).into(holder.imgPost);
;
@Override
public int getItemCount()
return 0;
public class MyViewHolder extends RecyclerView.ViewHolder
TextView tvTitle;
TextView tvName;
ImageView imgPost;
TextView tag;
CircleImageView imgUserPick;
public MyViewHolder(@NonNull @NotNull View itemView)
super(itemView);
tvTitle = itemView.findViewById(R.id.title);
tvName = itemView.findViewById(R.id.username);
tag = itemView.findViewById(R.id.tag);
imgPost = itemView.findViewById(R.id.imgPost);
imgUserPick = itemView.findViewById(R.id.userPick);
后模型
ublic class Post
private String title;
private String username;
private String postImage;
private String tag;
private String userId;
private String userPick;
private String description;
private String location;
private String wallet;
private Object time_stamp;
private String postKey;
public Post(String title, String username, String postImage, String tag, String userId, String userPick, String description, String location, String wallet)
this.username = username;
this.title = title;
this.tag = tag;
this.userId = userId;
this.userPick = userPick;
this.description = description;
this.wallet = wallet;
this.postImage = postImage;
this.location = location;
this.time_stamp = DocumentTransform.FieldTransform.ServerValue.REQUEST_TIME;
public Post()
public String getPostKey()
return postKey;
public void setPostKey(String postKey)
this.postKey = postKey;
public String getTitle()
return title;
public String getUsername()
return username;
public String getPostImage()
return postImage;
public String getTag()
return tag;
public String getUserId()
return userId;
public String getUserPick()
return userPick;
public String getDescription()
return description;
public String getLocation()
return location;
public String getWallet()
return wallet;
public Object getTime_stamp()
return time_stamp;
public void setTitle(String title)
this.title = title;
public void setUsername(String username)
this.username = username;
public void setPostImage(String postImage)
this.postImage = postImage;
public void setTag(String tag)
this.tag = tag;
public void setUserId(String userId)
this.userId = userId;
public void setUserPick(String userPick)
this.userPick = userPick;
public void setDescription(String description)
this.description = description;
public void setLocation(String location)
this.location = location;
public void setWallet(String wallet)
this.wallet = wallet;
public void setTime_stamp(Object time_stamp)
this.time_stamp = time_stamp;
【问题讨论】:
首先,停止忽略错误。使用Log.d(TAG, error.getMessage());
。您是否在 logcat 中打印了一些内容?除此之外,这段代码中究竟有什么不符合您的预期?
【参考方案1】:
adapter = new PostAdapter(context, postList);
PostRV.setAdapter(adapter);
//check size of postList using log.i
//在recyclerview适配器中设置项目计数为mData.size()
@Override
public int getItemCount()
return mData.Size();
【讨论】:
以上是关于Recycler View 不显示数据的主要内容,如果未能解决你的问题,请参考以下文章
Annotations Recycler View ItemClick
如何在 Fragment 中使用 Recycler View [重复]