英文:
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
英文:
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)
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论