gpt4 book ai didi

android - 在 recyclerview 中只显示一条评论 - Firebase

转载 作者:行者123 更新时间:2023-11-29 23:54:30 24 4
gpt4 key购买 nike

在我的应用程序中,注册用户可以成功发表评论。现在在我的代码中,我进行检查。如果特定新闻标题有评论,则填充回收 View 。如果没有,则显示一个 TextView ,说嘿,没有发表评论。但是,只显示一条评论而不是总数。

这就是我所说的支票。

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

成功返回数组列表的大小。 IE。 10 条评论 ArrayList 大小为 10。所以那部分很好。

logcat 也给我另一条消息。

E/RecyclerView: No adapter attached; skipping layout

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

发现 DisplayComments Activity here .

还有我的适配器代码here .

single_comment_row.xml 是 here

谢谢,

西奥。

最佳答案

你需要在循环外设置你的适配器

 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"

关于android - 在 recyclerview 中只显示一条评论 - Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50522516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com