Recyclerview 不会从 firebase 检索数据

Posted

技术标签:

【中文标题】Recyclerview 不会从 firebase 检索数据【英文标题】:Recyclerview doesn't retrieve data from firebase 【发布时间】:2021-09-01 21:55:24 【问题描述】:

我在 firebase 中添加了数据。我所要做的就是在 recyclerview 中获取这些数据。我做了这么多类型,但这次它没有显示,我不知道原因,因为它没有显示在日志中。有人可以帮忙吗?

这是 RV 所在的我的 Activity rvsalonlist 是 recyclerview

public void firebasedata() 
        FirebaseRecyclerOptions<salonList> options =
                new FirebaseRecyclerOptions.Builder<salonList>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("salon"), salonList.class)
                        .build();

        adapter = new SalonListAdapter(options);
        rvSalonList.setAdapter(adapter);

        adapter.startListening();

    

这是适配器

public class SalonListAdapter extends FirebaseRecyclerAdapter<salonList,SalonListAdapter.myviewholder> 


    public SalonListAdapter(@NonNull FirebaseRecyclerOptions<salonList> options) 
        super(options);
    

    @Override
    protected void onBindViewHolder(@NonNull myviewholder holder, int position, @NonNull salonList model) 
        holder.tvSalonName.setText(String.valueOf(model.getSalonName()));
        holder.tvSalonAddress.setText(String.valueOf(model.getSalonAddresss()));
        Glide.with(holder.ivSalonImage.getContext()).load(model.getImageUrl()).into(holder.ivSalonImage);
    

    @NonNull
    @Override
    public myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_salon,parent,false);
        return new myviewholder(view);
    

    class myviewholder extends RecyclerView.ViewHolder
        ImageView ivSalonImage;
        TextView tvSalonName, tvSalonMobileNumber, tvSalonAddress;

        public myviewholder(@NonNull View itemView) 
            super(itemView);

            ivSalonImage = itemView.findViewById(R.id.ivSalonImage);
            tvSalonName = itemView.findViewById(R.id.tvSalonName);
            tvSalonMobileNumber = itemView.findViewById(R.id.tvSalonMobileNumber);
            tvSalonAddress = itemView.findViewById(R.id.tvSalonAddress);
        
    


这是模型类。

[![public class salonList 

    private String imageUrl, salonName, ownerName, salonEmail, salonMobileNumber, salonAddresss, salonOpenTime, salonCloseTime;

    public salonList() 

    

    public salonList(String imageUrl, String salonName, String ownerName, String salonEmail, String salonMobileNumber, String salonAddresss, String salonOpenTime, String salonCloseTime) 
        this.imageUrl = imageUrl;
        this.salonName = salonName;
        this.ownerName = ownerName;
        this.salonEmail = salonEmail;
        this.salonMobileNumber = salonMobileNumber;
        this.salonAddresss = salonAddresss;
        this.salonOpenTime = salonOpenTime;
        this.salonCloseTime = salonCloseTime;
    

    public String getImageUrl() 
        return imageUrl;
    

    public void setImageUrl(String imageUrl) 
        this.imageUrl = imageUrl;
    

    public String getSalonName() 
        return salonName;
    

    public void setSalonName(String salonName) 
        this.salonName = salonName;
    

    public String getOwnerName() 
        return ownerName;
    

    public void setOwnerName(String ownerName) 
        this.ownerName = ownerName;
    

    public String getSalonEmail() 
        return salonEmail;
    

    public void setSalonEmail(String salonEmail) 
        this.salonEmail = salonEmail;
    

    public String getSalonMobileNumber() 
        return salonMobileNumber;
    

    public void setSalonMobileNumber(String salonMobileNumber) 
        this.salonMobileNumber = salonMobileNumber;
    

    public String getSalonAddresss() 
        return salonAddresss;
    

    public void setSalonAddresss(String salonAddresss) 
        this.salonAddresss = salonAddresss;
    

    public String getSalonOpenTime() 
        return salonOpenTime;
    

    public void setSalonOpenTime(String salonOpenTime) 
        this.salonOpenTime = salonOpenTime;
    

    public String getSalonCloseTime() 
        return salonCloseTime;
    

    public void setSalonCloseTime(String salonCloseTime) 
        this.salonCloseTime = salonCloseTime;
    

数据库截图

【问题讨论】:

【参考方案1】:

您的模型类变量名称和 firebase 属性名称不同。在两个地方使用相同的名称。 例如在 firebase 中使用 salonName 而不是名称。其他属性也一样

【讨论】:

【参考方案2】:

名称必须相同 例如,您在课堂上有ownerName,但在数据库中有ownername。这些都应该和你班上的一样。

【讨论】:

以上是关于Recyclerview 不会从 firebase 检索数据的主要内容,如果未能解决你的问题,请参考以下文章

无法从firebase实时数据库检索数据到recyclerview [重复]

从firebase获取数据时Recyclerview为空白

从 firebase 检索数据并将其显示在可扩展的 recyclerview 中

通过kotlin ..通过firebase从数据库读取数据到RecyclerView的麻烦

将数据从 Firebase 检索到嵌入在 Fragment 上的 RecyclerView

Android-从Firebase实时数据库读取数据并显示在RecyclerView上