简体   繁体   English

无法在 RecyclerView 中显示来自 firebase 的特定数据

[英]Failed to show specific data from firebase in a RecyclerView

I managed to retrieve the data from the firebase and use a recylcerview and a holder to display them.我设法从 firebase 中检索数据,并使用 recyclerview 和支架来显示它们。 I only need to retrieve data from certain nodes from the firebase.我只需要从 firebase 中的某些节点检索数据。 Nodes that are not taken by id but by an inside name:不是由 id 而是由内部名称获取的节点:

在此处输入图像描述

The problem is that it only displays my data for certain nodes, but it displays them in the order in which they are found in the database.问题是它只显示某些节点的数据,但它以它们在数据库中的顺序显示它们。 This leaves empty rows in the recyclerview.这会在回收站视图中留下空行。 (for example if the second node is the one you are looking for, put an empty card then the one with data): (例如,如果第二个节点是您要查找的节点,则放一张空卡,然后放一张有数据的卡):

在此处输入图像描述

The function by which I take the data from the firebase depending on the name is:我根据名称从 firebase 中获取数据的 function 是:

 private void LoadFeedbackMuzee(String denumire) { options = new FirebaseRecyclerOptions.Builder<Parere>().setQuery(refM, Parere.class).build(); adapterFM = new FirebaseRecyclerAdapter<Parere, MyViewHolderRecenziiM>(options) { @Override protected void onBindViewHolder(@NonNull MyViewHolderRecenziiM holder, int position, @NonNull Parere model) { if (model.getDenumire().equals(denumire)) { String userId = model.getIdUser(); int photo = model.getLevel(); holder.parere.setText(model.getIntrb1()); userRef.child(userId).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { if (snapshot.hasChild("fullname")) { String nume = snapshot.child("fullname").getValue().toString(); holder.username.setText(nume); } } @Override public void onCancelled(@NonNull DatabaseError error) { } }); if (photo == 1) { holder.image.setImageResource(R.drawable.terrible); } else if (photo == 2) { holder.image.setImageResource(R.drawable.bad); } else if (photo == 3) { holder.image.setImageResource(R.drawable.okay); } else if (photo == 4) { holder.image.setImageResource(R.drawable.good); } else if (photo == 5) { holder.image.setImageResource(R.drawable.great); } } } @NonNull @Override public MyViewHolderRecenziiM onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.parere_layout, parent, false); return new MyViewHolderRecenziiM(view); } }; adapterFM.startListening(); recyclerViewR.setAdapter(adapterFM); }

This is happening because you are doing these things in the onBindViewHolder .发生这种情况是因为您在onBindViewHolder中执行这些操作。 So in this case what happens is, if your condition matches then the data is binded otherwise the item in the recycler is left empty means the item layout is just inflated and if you know the working of the adapter then you must be knowing that the size of the list is used to inflate the number of items (parere_layout) in recycler view.因此,在这种情况下,如果您的条件匹配,则数据被绑定,否则回收站中的项目为空意味着项目布局只是膨胀,如果您知道适配器的工作,那么您必须知道大小列表的数量用于增加回收站视图中的项目数量(parere_layout) So the adapter inflates the layout but when it comes to binding (in onBindViewHolder) the data like userName and photo then if your defined condition matches then it binds otherwise the fields are left empty.因此,适配器膨胀了布局,但是当涉及到绑定(in onBindViewHolder)用户名和照片等数据时,如果您定义的条件匹配,那么它将绑定,否则字段为空。 Hope now you understand the working of the adapter.希望现在您了解适配器的工作原理。

So the solution is:所以解决方案是:

(1) that either you make itemView Visible for only your condition and make its visibility gone for unwanted condition. (1) 要么您使 itemView 仅在您的条件下可见,并使其可见性在不需要的条件下消失。 Sample code below下面的示例代码

holder.itemView.setVisibility(View.GONE);// to make it's visibility gone
 holder.itemView.setVisibility(View.VISIBLE);// to make it visible when condition matches.

(2) Recommended Solution: The second solution that I can see is that, if you filter your data before passing it to the adapter then your problem will be solved. (2)推荐解决方案:我可以看到的第二种解决方案是,如果您在将数据传递给适配器之前对其进行过滤,那么您的问题将得到解决。 By filtering, I mean that after fetching data from the database you just make another list in which you add only that data what you want to be shown, so in this way, your adapter code will be more cleaner and also your problem will be solved simultaneously.通过过滤,我的意思是在从数据库中获取数据后,您只需创建另一个列表,在其中仅添加您想要显示的数据,因此通过这种方式,您的适配器代码将更清晰,您的问题也将得到解决同时。

Feel free to ask if something is unclear.随意询问是否有不清楚的地方。 And kindly mark this as the correct answer if it helps you so that in the future this answer can also help any other needy.如果对您有帮助,请将其标记为正确答案,以便将来此答案也可以帮助任何其他有需要的人。

I THINK you have to do something to your views if data doesn't exist at a node but is still being looped through.如果节点上不存在数据但仍在循环中,我认为您必须对视图做一些事情。 So you would do something like this where you would make the views GONE if the denumire doesn't.equal(model.getDenumire) and VISIBLE if it does.所以你会做这样的事情,如果 denumire doesn't.equal(model.getDenumire) 和 VISIBLE 如果它确实,你会让视图消失。 And do the same if the snapshot.hasChild(fullname) doesn't have the name so like:如果 snapshot.hasChild(fullname) 没有这样的名称,请执行相同操作:

   if (model.getDenumire().equals(denmuire) {
            GETDATA()
            if (snapshot.hasChild(fullname) {

                holder.image.setVisibility(View.VISIBLE);
                holder.name..setVisibility(View.VISIBLE);
                holder.name.setName(Name)
                   if (photo == 1) {
                    holder.image.setImageResource(R.drawable.terrible);
                } else if (photo == 2) {
                    holder.image.setImageResource(R.drawable.bad);
                } else if (photo == 3) {
                    holder.image.setImageResource(R.drawable.okay);
                } else if (photo == 4) {
                    holder.image.setImageResource(R.drawable.good);
                } else if (photo == 5) {
                    holder.image.setImageResource(R.drawable.great);
                }

            }else{
                holder.image.setVisibility(View.GONE);
                holder.name.setVisibility(View.GONE);
            }

        } else{
            holder.image.setVisibility(View.GONE);
            holder.name.setVisibility(View.GONE);
            etc...
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM