java - 如何从模型类中检索全部子级并在android中传入适配器?
问题描述
我从 firebase 实时数据库中检索数据并将其传递给适配器并在 recyclerview 中显示。下图显示了我的数据结构。
模型类成功检索了姓氏、邮政ID 和时间戳。但我想检索总数。“评论”的孩子并传入适配器。这是我的模型课。
public class Post
{
public String lastname;
public String postid;
public long timestamp;
public Postt()
{
}
public Postt(String lastname, long timestamp, String postid)
{
this.lastname=lastname;
this.timestamp=timestamp;
this.postid=postid;
}
public String getPostid() {
return postid;
}
public void setPostid(String postid) {
this.postid = postid;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
适配器是
List<Post> mPost;
@Override
public void onBindViewHolder(@NonNull final PostHolder postHolder, final int i) {
String posid=mPost.get(i).getPostid();
String lastname=mPost.get(i).getLastname();
long timestamp=mPost.get(i).getTimestamp();
***//Here i want to get the total no of child of "comments" node.//
//please dont tell to use ValueEventListner inside onBindViewHolder//***
}
这是我在 MainActivity 中的查询
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Post> userModels = new ArrayList<>();
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
userModels.add(userSnapshot.getValue(Post.class));
//Here I want to retrieve no of "comments"child and pass in adapter//
}
mAdapter.addAll(userModels);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
用于检索“评论”总数。孩子,我如何在查询中准备模型类和for循环?
解决方案
由于您使用的是 firebase 数据库,我相信您正在使用 datasnapshot 来获取特定帖子的评论,如您在数据库结构中显示的那样。当您在添加监听器后获取 datasnapshot 时,您会发现dataSnapshot将具有 children Count 方法,您可以这样调用它:
dataSnapshot.getChildrenCount()
在您从中获取评论的节点中使用它,然后您可以在将列表发送到适配器后发送。
编辑:
您现在实际上获得了整个 Post 节点,而您只缺少评论部分,您将需要在您的 Post 类中添加带有变量名评论的 List 以及它的 setter 和 getter。喜欢这里,但将 Comment 更改为评论 String Object 的类型,无论您将评论作为数据类型
public class Post
{
public String lastname;
public String postid;
public long timestamp;
public HashMap<String, YourCommentClassName> comments;
在你的 onBindViewHolder 中会是这样的
List<Post> mPost;
@Override
public void onBindViewHolder(@NonNull final PostHolder postHolder, final int i) {
String posid=mPost.get(i).getPostid();
String lastname=mPost.get(i).getLastname();
long timestamp=mPost.get(i).getTimestamp();
// This will have the list of comments and the number of the comments
//you have
long commentsNumber = 0
if(mPost.get(i).getComments()!=null)
commentsNumber=mPost.get(i).getComments().size();
}
推荐阅读
- java - 从活动到原始非活动类的结果
- react-native - React Native 错误,Redux:键“nav”的切片缩减器在初始化期间返回未定义
- python - 如何在状态机中浏览状态时在全屏上显示不断更新的文本?
- angular - 如何将反应形式值作为对象内的对象
- python - 有没有办法使用 Python 将 epub2 转换为 epub3?
- python - 使用 python 更改 Google 电子表格单元格水平对齐方式
- haskell - 我的 recurvise haskell 代码有什么问题?
- javascript - 合并不同的数组并计算平均价格 Javascript
- javascript - 有没有办法从当前页面上的文件输入中使用文件
- javascript - 角色计数无法正常工作 discord.js