Java 如何在android中从模型类中检索全部子类并传入适配器?

Java 如何在android中从模型类中检索全部子类并传入适配器?,java,android,firebase-realtime-database,Java,Android,Firebase Realtime Database,我从firebase实时数据库检索数据,并将其传递给适配器,并在recyclerview中显示。 下图显示了我的数据结构。 模型类成功地检索了lastname、postid和timestamp。 但我想检索“comments”子节点的总数并传入适配器。 这是我的模型课 public class Post { public String lastname; public String postid; public long timestamp; public P

我从firebase实时数据库检索数据,并将其传递给适配器,并在recyclerview中显示。 下图显示了我的数据结构。

模型类成功地检索了lastname、postid和timestamp。 但我想检索“comments”子节点的总数并传入适配器。 这是我的模型课

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//***
            }
列表mPost;
@凌驾
public void onBindViewHolder(@NonNull final PostHolder PostHolder,final int i){
字符串posid=mPost.get(i).getPostid();
字符串lastname=mPost.get(i).getLastname();
long timestamp=mPost.get(i.getTimestamp();
***//这里我想得到“comments”节点的子节点的总数//
//请不要告诉在BindViewHolder中使用ValueEventListner//***
}
这是我在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) {
            }
        });
query.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
List userModels=new ArrayList();
对于(DataSnapshot userSnapshot:DataSnapshot.getChildren()){
添加(userSnapshot.getValue(Post.class));
//在这里,我想检索“comments”子项的编号并传入适配器//
}
addAll(用户模型);
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});

为了检索子级的“注释”总数,我如何在查询中准备模型类和For循环?

因为您使用的是firebase数据库 我相信您使用datasnapshot是为了获得数据库结构中显示的特定帖子的评论。 当您在添加侦听器后获得datasnapshot时,您会发现datasnapshot将具有子计数方法,您可以这样调用它:

dataSnapshot.getChildrenCount()

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

参考:

编辑:

您现在实际得到的是整个Post节点,而您只缺少注释部分,您需要在Post类中添加带有变量名注释的列表及其setter和getter。与此处类似,但将Comment更改为comments字符串对象的类型,无论您将Comment作为数据类型是什么

public class Post
{
public String lastname;
public String postid;
public long timestamp;
public HashMap<String, YourCommentClassName> comments;
公共类职位
{
公共字符串lastname;
公共字符串posted;
公共长时间戳;
公共哈希图评论;
在你的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();
        }
列表mPost;
@凌驾
public void onBindViewHolder(@NonNull final PostHolder PostHolder,final int i){
字符串posid=mPost.get(i).getPostid();
字符串lastname=mPost.get(i).getLastname();
long timestamp=mPost.get(i.getTimestamp();
//这将有评论列表和评论数量
//你有
long commentsNumber=0
if(mPost.get(i).getComments()!=null)
commentsNumber=mPost.get(i).getComments().size();
}

Hey guys请帮助meI思考注释使hashmap不是列表是相同的概念您将获得注释的hashmap.size(),并将其添加到Post model类,然后使用相同的方法在onBindViewHolder中检索@tailorOk我将尝试它YourCommentClass意味着注释模型类?