简体   繁体   English

Android Firebase RecyclerView适配器删除视图

[英]Android Firebase RecyclerView Adapter Remove Views

I am facing a Firebase RecyclerView problem where I cannot remove unwanted CardViews from my RecyclerViews. 我遇到了Firebase RecyclerView问题,无法从RecyclerViews中删除不需要的CardView。 In my code I check the city's name and the guide's chosen city to match them. 在我的代码中,我检查了城市的名称和向导选择的城市以匹配它们。 It populates guide's details only if the guide's city matches the picked city, but it also shows empty cardview with default layout. 仅当导游的城市与所选城市匹配时,它才会填充导游的详细信息,但它还会显示具有默认布局的空Cardview。

guideDataRef = FirebaseDatabase.getInstance().getReference().child("Guides");

public void recycler() {
        super.onStart();

        try {
            //Guide RecyclerView
            Query guideQuery = guideDataRef.orderByKey();
            guideQuery.keepSynced(true);

            FirebaseRecyclerOptions guideOptions =
                    new FirebaseRecyclerOptions.Builder<UserModelClass>().setQuery(guideQuery, UserModelClass.class).build();

            guideAdapter = new FirebaseRecyclerAdapter<UserModelClass, guideViewHolder>(guideOptions) {


                @Override
                protected void onBindViewHolder(@NonNull guideViewHolder holder, final int position, @NonNull final UserModelClass model) {

                    String pickedcity = model.getPickedCity();
                    String postname = (String) cityName.getText();

                    if(pickedcity.equals(postname)) {

                        final String guide_key= getRef(position).getKey();

                        holder.setGuideName(model.getName());
                        holder.setGuideSurname(model.getSurName());
                        holder.setGuideImage(getApplicationContext(), model.getPhotoURL());
                       // holder.mView.setVisibility(View.VISIBLE);

                        //Guide Click listener
                        holder.mView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                Intent guideHireIntent = new Intent(getApplication(), GuideHireActivity.class);
                                guideHireIntent.putExtra("guide_id", guide_key);
                                finish();
                                startActivity(guideHireIntent);

                            }
                        });

                    }

                }

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

                @Override
                public void onError(DatabaseError e){
                    Toast.makeText(getApplicationContext(), "Error by stopping ", Toast.LENGTH_SHORT).show();
                }

                @Override
                public int getItemCount() {
                    return super.getItemCount();
                }

                @Override
                public void onDataChanged() {
                    super.onDataChanged();
                    notifyDataSetChanged();
                }
            };

            guideAdapter.notifyDataSetChanged();
            guideRecyclerView.setAdapter(guideAdapter);
            guideAdapter.startListening();


        } catch (DatabaseException e) {

            Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();

        }

    }

enter image description here 在此处输入图片说明
enter image description here 在此处输入图片说明

I can change the adapter visibility to gone if it does not match with the requirements but the problem is that after making it's visibility gone it is still there holding the place (but invisible - there's still an empty space). 如果适配器的可见性与要求不匹配,我可以将其可见性更改为“消失”,但是问题是,使其可见性消失之后,它仍然可以容纳该位置(但是不可见-仍然有一个空白空间)。 How can I avoid populating an item from the recycler view completely, instead of making it invisible if the requirements do not match? 如何避免从回收者视图中完全填充项目,而不是在需求不匹配的情况下使其不可见?

You're not showing what guideDataRef is in your code, so I'm assuming that it's just a DatabaseReference object for everything beneath a \\Guides node. 您没有显示代码中的guideDataRef ,所以我假设它只是\\Guides节点下所有内容的DatabaseReference对象。

If you're doing that, you're going to get a call for onBindViewHolder for every child at that particular location. 如果这样做,您将得到该特定位置的每个孩子的onBindViewHolder调用。 This means that you're going to be asked to make a view for every child. 这意味着将要求您为每个孩子做一个view You cannot choose whether or not a view will appear for that item. 您无法选择是否为该项目显示视图。

It looks like you're assuming that your if statement in onBindViewHolder method will skip over those items. 似乎您假设onBindViewHolder方法中的if语句将跳过这些项目。 But what's actually happening is that you're simply allowing an empty view to occupy that spot in the list. 但是实际上发生的是,您只是在允许空白视图占据列表中的该位置。

Instead, you should come up with a query that generates only the items of interest to your list. 相反,您应该提出一个查询 ,该查询仅生成列表中感兴趣的项目。 This means you'll have to tell Firebase to filter for children that meet your criteria. 这意味着您必须告诉Firebase 筛选符合条件的孩子

You can also read the entire contents of the location, manually filter out the items you don't want, and build a list of items you do want. 您还可以阅读该位置的全部内容,手动过滤掉不需要的项目,并构建需要的项目列表。 You can then build an custom adapter with that list, and it can then become the input to a ListView or even better to a RecyclerView . 然后,您可以使用该列表构建自定义适配器,然后它可以成为ListView的输入,甚至可以成为RecyclerView

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

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