Java 为什么firebase无法在recycler视图中查询收集的数据?

Java 为什么firebase无法在recycler视图中查询收集的数据?,java,android,database,firebase,comments,Java,Android,Database,Firebase,Comments,我可以在firebase collection中获取数据,但它不会在recycler视图中查询该数据。Recyclerview没有显示任何内容 这是注释列表类 public class comment_list { public comment_list(String comments) { this.comments = comments; } public String getComments() { return comments;

我可以在firebase collection中获取数据,但它不会在recycler视图中查询该数据。Recyclerview没有显示任何内容

这是注释列表类

public class comment_list {
    public comment_list(String comments) {
        this.comments = comments;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    String comments;
}
这是注释\u适配器类

public class comment_adapter extends FirestoreRecyclerAdapter<comment_list, comment_adapter.comment_holder> {
  
    public comment_adapter(@NonNull FirestoreRecyclerOptions<comment_list> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull comment_holder holder, int position, @NonNull comment_list model) {
        holder.commment_on_post.setText(model.getComments());
    }

    @NonNull
    @Override
    public comment_holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_recycler_dsign, parent, false);
        return new comment_holder(v);
    }

    public class comment_holder extends RecyclerView.ViewHolder{
      TextView commment_on_post;
        public comment_holder(@NonNull View itemView) {
            super(itemView);
            commment_on_post = itemView.findViewById(R.id.commenttextview);
        }
    }
公共类注释\u适配器扩展FirestoreRecyclerAdapter{
公共注释\u适配器(@NonNull FirestoreRecyclerOptions选项){
超级(期权);
}
@凌驾
受保护的无效onBindViewHolder(@NonNull comment\u holder,int position,@NonNull comment\u list model){
post.setText(model.getComments())上的holder.commment;
}
@非空
@凌驾
public comment_holder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.comment\u recycler\u dsign,parent,false);
返回新的注释持有者(v);
}
公共类注释持有者扩展了RecyclerView.ViewHolder{
发布文本视图命令;
公共注释持有者(@NonNull View itemView){
超级(项目视图);
post上的commment=itemviewbyd(R.id.commenttextview);
}
}
这是Comments类。在此类中,我可以在firebase集合中获取数据,但在recycler视图中无法查询该数据

public class Comments extends AppCompatActivity {
 ImageView profileimage;
 EditText addcommenttext;
 TextView postcommenttext;
    FirebaseFirestore db;
  
    RecyclerView comment_recycler_view;

   comment_adapter adaptercomment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments);

        profileimage = findViewById(R.id.Addcommentprofileimage);
        addcommenttext = findViewById(R.id.addcommenttext);
        postcommenttext = findViewById(R.id.postcomment);
    
comment_recycler_view = findViewById(R.id.commentsrecycler);




        postcommenttext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (addcommenttext.equals("")) {
                    Toast.makeText(Comments.this, "Comment can't be empty", Toast.LENGTH_SHORT).show();
                } else {
                    String commentText = addcommenttext.getText().toString();

                    CollectionReference commentref = FirebaseFirestore.getInstance() .collection("CommentDetails");
                    commentref.add(new comment_list(commentText));

         

                    
                    FirebaseFirestore fbfs = FirebaseFirestore.getInstance();
                    CollectionReference commentrefs = fbfs.collection("CommentDetails");
                    Query query = commentrefs;

                    FirestoreRecyclerOptions<comment_list> options = new FirestoreRecyclerOptions.Builder<comment_list>()
                            .setQuery(query, comment_list.class)
                            .build();
                    adaptercomment = new comment_adapter(options);

                    comment_recycler_view.setHasFixedSize(true);
                    comment_recycler_view.setLayoutManager(new LinearLayoutManager(getApplication()));
                    comment_recycler_view.setAdapter(adaptercomment);
                    finish();
                    Toast.makeText(Comments.this, "Commented", Toast.LENGTH_SHORT).show();

                }
            }


    });

}
公共类注释扩展了AppCompatActivity{
图像视图轮廓图像;
编辑文本添加评论文本;
文本查看后公共文本;
FirebaseFirestore数据库;
回收者视图注释\回收者视图;
评论(u adapter adapter comment);;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_注释);
profileimage=findviewbyd(R.id.Addcommentprofileimage);
addcommenttext=findViewById(R.id.addcommenttext);
postcommenttext=findViewById(R.id.postcomment);
comment\u recycler\u view=findViewById(R.id.commentsrecycler);
postcommenttext.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(addcommenttext.equals(“”){
Toast.makeText(Comments.this,“Comment不能为空”,Toast.LENGTH_SHORT.show();
}否则{
String commentText=addcommenttext.getText().toString();
CollectionReference commentref=FirebaseFirestore.getInstance().collection(“CommentDetails”);
commentref.添加(新注释列表(commentText));
FirebaseFirestore fbfs=FirebaseFirestore.getInstance();
CollectionReference commentrefs=fbfs.collection(“CommentDetails”);
Query=commentrefs;
FirestoreRecyclerOptions选项=新建FirestoreRecyclerOptions.Builder()
.setQuery(查询,注释列表.class)
.build();
adaptercomment=新注释\适配器(选项);
注释\回收器\视图。setHasFixedSize(true);
comment_recycler_view.setLayoutManager(新的LinearLayoutManager(getApplication());
注释\u recycler\u view.setAdapter(adaptercomment);
完成();
Toast.makeText(Comments.this,“Commented”,Toast.LENGTH_SHORT.show();
}
}
});
}

首先,让我们重新配置您的Comments活动类。建议在您的
onCreate
方法中初始化回收适配器,而不是在重写的
onClick
方法中。根据当前设置,每次触发onClick侦听器时都会初始化一个新的comment\u适配器。最好仅列出一个。以下是更改后的情况(为了清晰起见,我添加了注释):

注意:为了清晰起见,我重新命名了类、变量和方法,以使用java和android约定。学习这些将有助于你阅读其他代码,并为你自己的代码省去很多麻烦

这里唯一的区别是firestore需要一个零参数(无参数)构造函数


我还没有看到您的视图模型项布局(
comment\u recycler\u dsign
),但请检查根布局的高度是否不为“match\u parent”。这是一个常见错误。如果只显示一个回收项目,则最好先检查此选项。

首先,让我们重新配置您的Comments活动类。建议在
onCreate
方法中初始化回收适配器,而不是在重写的
onClick
方法中初始化回收适配器。使用当前设置,每次触发onClick侦听器时,都会初始化一个新的注释\u适配器。最好只设置一个。以下是更改后的情况(为了清晰起见,我添加了注释):

注意:为了清晰起见,我重新命名了类、变量和方法,以使用java和android约定。学习这些将有助于你阅读其他代码,并为你自己的代码省去很多麻烦

这里唯一的区别是firestore需要一个零参数(无参数)构造函数

明智的一句话-我没有看到您的视图模型项布局(
comment\u recycler\u dsign
),但只需检查根布局的高度是否没有“match\u parent”。这是一个常见错误。如果您只看到一个循环项被呈现,最好先检查此选项。

放置一个侦听器 上传完成后,fire base将自动调用

 firestore.collection("").add(Any()).addOnCompleteListener { 
                    // do all your work here
 }
安排一位听众 上传完成后,fire base将自动调用

 firestore.collection("").add(Any()).addOnCompleteListener { 
                    // do all your work here
 }

请发布您的数据库结构数据库中的数据您在cloud firestore中的平均收集量?请编辑您的问题并将您的数据库结构添加为屏幕
 firestore.collection("").add(Any()).addOnCompleteListener { 
                    // do all your work here
 }