Firebase 适配器未根据我的需要更新数据

Firebase adapter is not updating the data according to my needs

我的应用程序有一些用户存储在 firebase 实时数据库中,每个用户都有一个节点“showGigCount”,如果有人点击该用户,该节点应该更新。 问题: 如果用户 1 单击用户 2 的数据,则用户 1 的 showGigCount 将递增 1,用户 2 的 showGigCount 将递减 1。正如您在代码中看到的,我可以成功更新用户 1 的 showGigCount 值但用户 2 的值没有改变。任何建议将不胜感激。下面是我的代码。谢谢大家

DashboardActivity

    fAuth = FirebaseAuth.getInstance();
            firebaseDatabase = FirebaseDatabase.getInstance();
            dRef = FirebaseDatabase.getInstance().getReference("Users");
    
            if(fAuth.getCurrentUser() != null){
                UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
            }
    
                dRef.child(UID).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        remainingClicks = Integer.parseInt(String.valueOf(snapshot.child("clicksRemain").getValue()));
                        showGigCount =  Integer.parseInt(String.valueOf(snapshot.child("showGigCount").getValue()));
                        String username =  String.valueOf(snapshot.child("name").getValue());
                        clicks.setText(String.valueOf(remainingClicks));
                        userName.setText(username);
                    }
                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
    
                    }
                });
    
            recyclerView = findViewById(R.id.dashboardRCV);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
          LoadData();
        }
    
        private void LoadData() {
            // This will display the data of a user if his showGigCount is greater than 0.
            DatabaseReference db = FirebaseDatabase.getInstance().getReference();
            DatabaseReference usersRef = db.child("Users");
            Query queryByShowGigCount = usersRef.orderByChild("showGigCount").startAt(1);
            options = new FirebaseRecyclerOptions.Builder<ModelClass>()
                    .setQuery(queryByShowGigCount, 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());
                        }
                    });
                    String user_clicked = getRef(position).getKey();
    
                    if (user_clicked.equals(UID)){
                        holder.previewLink.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Log.d("if1 working", "it's showing the data");
                                Toast.makeText(DashboardActivity.this, "Opening your link is not allowed", Toast.LENGTH_LONG).show();
                            }
                        });
                    }else if (!user_clicked.equals(UID)){
                        holder.previewLink.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                            Log.d("Online User", "Online user clicking");
                                if (remainingClicks >= 1) { // This code increment the showGigCount of User 1 by 1.
                                    remainingClicks--;
                                    showGigCount ++;
                                    clicks.setText(String.valueOf(remainingClicks));
                                    UserClicksCounts();
                                    UserShowGigCounts();
                                    
                                    // I wrote this code so that showGigCount of User 2 will be decreased by 1 but it's not working
                                    dRef.child(String.valueOf(user_clicked)).addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(@NonNull DataSnapshot snapshot) {
    
                                            showGigCount--;
                                            UserGigCountDecrease();
                                            Log.d("CLicked User", "showGigCount decreased");
                                        }
    
                                        @Override
                                        public void onCancelled(@NonNull DatabaseError error) {
    
                                        }
                                    });
                                } else if (remainingClicks == 0){
                                    zeroClicks.setVisibility(View.VISIBLE);
                                }
                            }
                        });
                    }
                    }
    
                @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);
        }
        void UserClicksCounts(){
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("Users").child(UID);
            myRef.child("clicksRemain").setValue(remainingClicks);
        }
    // This will count how many times the gig will be shown to the user
        void UserShowGigCounts(){
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("Users").child(UID);
            myRef.child("showGigCount").setValue(showGigCount);

void UserGigCountDecrease(){
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("Users").child(user_clicked);
        myRef.child("showGigCount").setValue(showGigCount);
    }

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;
    }

为了解决这个问题,我在 OnCLickListener

中添加了这些行
> user_clicked = getRef(position).getKey();
>                                 assert user_clicked != null;
>                                 dRef.child(user_clicked).child("showGigCount").setValue(model.getShowGigCount()-1);

现在已成功减少用户 2 的 showGigCount。 我还删除了以下代码行

dRef.child(String.valueOf(user_clicked)).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) {

                                    showGigCount--;
                                    UserGigCountDecrease();
                                    Log.d("CLicked User", "showGigCount decreased");
                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError error) {

                                }
                            });