首页 > 解决方案 > 如何从模型类中检索全部子级并在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循环?

标签: javaandroidfirebase-realtime-database

解决方案


由于您使用的是 firebase 数据库,我相信您正在使用 datasnapshot 来获取特定帖子的评论,如您在数据库结构中显示的那样。当您在添加监听器后获取 datasnapshot 时,您会发现dataSnapshot将具有 children Count 方法,您可以这样调用它:

dataSnapshot.getChildrenCount()

在您从中获取评论的节点中使用它,然后您可以在将列表发送到适配器后发送。

参考:DataSnapshot_Docs

编辑:

您现在实际上获得了整个 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();
        }

推荐阅读