Gum Naam
Gum Naam

Reputation: 154

How to make firebase recycler adapter to show specific data on some conditions

I need to know how can I display data on specific condition i.e. display the data of a user if his showGigCount is greater than 0. I have used Query to achieve this but by using it all the data disappear. If the marked node become zero only data of that specific user must not be displayed. Below is my code. Thank You all

MyViewHolder.Class

public MyViewHolder(@NonNull @NotNull View itemView) { super(itemView);

    previewLink =  itemView.findViewById(R.id.uev);
    geglink = (TextView) itemView.findViewById(R.id.rcvGigLink);
    userName = (TextView) itemView.findViewById(R.id.rcvName);
    userAbout = (TextView) itemView.findViewById(R.id.rcvAbout);
    v = itemView;

DashboardActivity.class

fAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();
        dRef = FirebaseDatabase.getInstance().getReference("Users");

        recyclerView = findViewById(R.id.dashboardRCV);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
      LoadData();

    }

    private void LoadData() {
        options = new FirebaseRecyclerOptions.Builder<ModelClass>()
                .setQuery(dRef, ModelClass.class)
                .build();
        adapter = new FirebaseRecyclerAdapter<ModelClass, MyViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull ModelClass model) {

           
                
                holder.previewLink.setURL(model.getGig(), new URLEmbeddedView.OnLoadURLListener() {
                    @Override
                    public void onLoadURLCompleted(URLEmbeddedData data) {

                        holder.previewLink.title(data.getTitle());
                        holder.previewLink.description(data.getDescription());
                        holder.previewLink.host(data.getHost());
                        holder.previewLink.thumbnail(data.getThumbnailURL());
                        holder.previewLink.favor(data.getFavorURL());
                    }
                });


                //This will hide the gig if its showGigCount becomes 0
                Query query = dRef.orderByChild("showGigCount").equalTo(0);
                ValueEventListener valueEventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        if (snapshot.exists()){

                            holder.previewLink.setVisibility(View.GONE);
                        }
                    }
                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                    }
                };
                query.addListenerForSingleValueEvent(valueEventListener);



            }

            @NonNull
            @Override
            public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_layout, parent, false);
                return new MyViewHolder(view);
            }
        };
        adapter.startListening();
        recyclerView.setAdapter(adapter);

Upvotes: 0

Views: 549

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138804

display the data of a user if his showGigCount is greater than 0.

To achieve that, please use the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = db.child("Users");
Query queryByShowGigCount = usersRef.orderByChild("showGigCount").startAt(0).startAt(1);
options = new FirebaseRecyclerOptions.Builder<ModelClass>()
            .setQuery(queryByShowGigCount, ModelClass.class)
            .build();

As you can see the key to solving this problem is the use of the Query#startAt() method.

Upvotes: 2

Related Questions