回收器视图中仅显示一条评论 - Firebase



在我的应用程序中,注册用户可以成功发表评论。现在在我的代码中,我做了一个检查。如果特定新闻标题有评论,则填充回收视图。如果没有,则显示一个文本视图,说嘿,没有发布任何评论。但是,仅显示一条注释,而不是总计。

这就是我所说的支票。

Query query = mDatabase.child("comments").orderByChild("newsTitle").equalTo(newsTitle);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot dataSnapshots : dataSnapshot.getChildren()){
Comment comment = dataSnapshots.getValue(Comment.class);
//mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(userId);
commentArrayList.add(comment);
mRecyclerView.setAdapter(displayCommentsAdapter);
displayCommentsAdapter = new DisplayCommentsAdapter(getApplication(),commentArrayList);
displayCommentsAdapter.notifyDataSetChanged();
//displayCommentsAdapter.setCommentsData(commentArrayList);
Log.d(LOG_TAG, "commentArrayList size: " + String.valueOf(commentArrayList.size()));
}
//commentsTextView.setVisibility(View.VISIBLE);
}else{
//Toast.makeText(DisplayComments.this,"There are no comments posted",Toast.LENGTH_LONG).show();
noCommentsTextView.setVisibility(View.VISIBLE);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});

Log.d(LOG_TAG, "commentArrayList size: " + 
String.valueOf(commentArrayList.size()));

成功返回数组列表的大小。 即 10 条注释 数组列表大小为 10。所以这部分很好。

日志猫也给了我另一个消息。

E/RecyclerView: No adapter attached; skipping layout

也许这与我目前的问题有关。

"显示注释"活动可在此处找到。

我的适配器代码在这里。

single_comment_row.xml在这里

谢谢

西欧。

您需要将适配器设置在循环之外

query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot dataSnapshots : dataSnapshot.getChildren()){
Comment comment = dataSnapshots.getValue(Comment.class);
//mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(userId);
commentArrayList.add(comment);

//displayCommentsAdapter.setCommentsData(commentArrayList);
Log.d(LOG_TAG, "commentArrayList size: " + String.valueOf(commentArrayList.size()));
}
mRecyclerView.setAdapter(displayCommentsAdapter);
displayCommentsAdapter = new DisplayCommentsAdapter(getApplication(),commentArrayList);
displayCommentsAdapter.notifyDataSetChanged();
//commentsTextView.setVisibility(View.VISIBLE);
}else{
//Toast.makeText(DisplayComments.this,"There are no comments posted",Toast.LENGTH_LONG).show();
noCommentsTextView.setVisibility(View.VISIBLE);
}

编辑

ConstraintLayout的高度更改为android:layout_height="wrap_content"

最新更新