RecyclerView 显示重复项。

huangapple go评论78阅读模式
英文:

Recycler View Showing Duplicate items

问题

[![在此输入图片描述][1]][1]

根据上面的图片,在我点击任何项目的关注按钮时,回收站视图会自动添加所有项目的更新值,如下图所示。(在这里,我点击了 UJjwal Pratap 的关注按钮)

[![在此输入图片描述][2]][2]

适配器

public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
final UserDataModel userDataModel = userDataModelList.get(position);
userName.setText(userDataModel.getUserName());
userFollowers.setText(userDataModel.getUserFollowers() + " Followers");

    followUser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(userDataModel.getUserID()).child("Followers")
                    .child(Objects.requireNonNull(FirebaseAuth.getInstance().getUid()));
            databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists() && Objects.requireNonNull(dataSnapshot.getValue()).toString().equals("1")) {
                        FirebaseDatabase.getInstance().getReference().child("Users").child(userDataModel.getUserID()).child("Followers")
                                .child(Objects.requireNonNull(FirebaseAuth.getInstance().getUid())).removeValue();
                        FirebaseDatabase.getInstance().getReference().child("Users").child(FirebaseAuth.getInstance().getUid())
                                .child("Following").child(userDataModel.getUserID()).removeValue();
                    } else {
                        FirebaseDatabase.getInstance().getReference().child("Users").child(userDataModel.getUserID()).child("Followers")
                                .child(Objects.requireNonNull(FirebaseAuth.getInstance().getUid())).setValue("1");
                        FirebaseDatabase.getInstance().getReference().child("Users").child(FirebaseAuth.getInstance().getUid())
                                .child("Following").child(userDataModel.getUserID()).setValue("1");
                    }
                }

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

                }
            });
        }
    });
}

片段

FirebaseDatabase.getInstance().getReference().child("Users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
if (!Objects.equals(childSnapshot.getKey(), FirebaseAuth.getInstance().getUid())) {
final UserDataModel userDataModel = childSnapshot.getValue(UserDataModel.class);
assert userDataModel != null;
userDataModel.setUserFollowers(String.valueOf(childSnapshot.child("Followers").getChildrenCount()));
PeopleArrayList.add(userDataModel);
}
}
PeopleAdapter = new PeopleAdapter(PeopleArrayList);
PeopleRecyclerView.setAdapter(PeopleAdapter);
}

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

        }
    });

  [1]: https://i.stack.imgur.com/ZvZQz.jpg
  [2]: https://i.stack.imgur.com/oZcXH.jpg
英文:

RecyclerView 显示重复项。

As of the above image, when I am clicking the follow button of any item then the recycler view is automatically adding updated values of all items twice as seen in the below image. (Here I clicked the follow button of UJjwal Pratap)

RecyclerView 显示重复项。

Adapter

public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final UserDataModel userDataModel = userDataModelList.get(position);
        userName.setText(userDataModel.getUserName());
        userFollowers.setText(userDataModel.getUserFollowers() + " Followers");

        followUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(userDataModel.getUserID()).child("Followers")
                        .child(Objects.requireNonNull(FirebaseAuth.getInstance().getUid()));
                databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists() && Objects.requireNonNull(dataSnapshot.getValue()).toString().equals("1")) {
                            FirebaseDatabase.getInstance().getReference().child("Users").child(userDataModel.getUserID()).child("Followers")
                                    .child(Objects.requireNonNull(FirebaseAuth.getInstance().getUid())).removeValue();
                            FirebaseDatabase.getInstance().getReference().child("Users").child(FirebaseAuth.getInstance().getUid())
                                    .child("Following").child(userDataModel.getUserID()).removeValue();
                        } else {
                            FirebaseDatabase.getInstance().getReference().child("Users").child(userDataModel.getUserID()).child("Followers")
                                    .child(Objects.requireNonNull(FirebaseAuth.getInstance().getUid())).setValue("1");
                            FirebaseDatabase.getInstance().getReference().child("Users").child(FirebaseAuth.getInstance().getUid())
                                    .child("Following").child(userDataModel.getUserID()).setValue("1");
                        }
                    }

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

                    }
                });
            }
        });
    }

Fragment

FirebaseDatabase.getInstance().getReference().child("Users").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                    if (!Objects.equals(childSnapshot.getKey(), FirebaseAuth.getInstance().getUid())) {
                        final UserDataModel userDataModel = childSnapshot.getValue(UserDataModel.class);
                        assert userDataModel != null;
                        userDataModel.setUserFollowers(String.valueOf(childSnapshot.child("Followers").getChildrenCount()));
                        PeopleArrayList.add(userDataModel);
                    }
                }
                PeopleAdapter = new PeopleAdapter(PeopleArrayList);
                PeopleRecyclerView.setAdapter(PeopleAdapter);
            }

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

            }
        });

答案1

得分: 0

创建一个名为 PeopleArrayList 的变量。

在设置适配器之前使用 peopleArrayList.clear

然后使用 peopleAdapter.notifySetDataChanged();

谢谢!愉快的编程!

英文:

Make a veriable of PeopleArrayList

Then use peopleArrayList.clear before setting adapter

Then use peopleAdapter.notifySetDataChanged();

Thankew! Happy Coding!

huangapple
  • 本文由 发表于 2020年5月4日 14:14:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61586167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定