我的 RecyclerView 复制了我的项目视图
My RecyclerView duplicates my items views
所以我正在测试 Recycler View 并在添加新项目时遇到一些麻烦。
我有一个 RecyclerView,我试图在其中显示用户的联系人,但是当我创建一个新元素时,所有联系人都重复了。我知道我应该在添加新数据之前清理我的视图,但我不确定该怎么做。
这是我的主要 activity 代码
for (DataSnapshot postSnapshot : snapshot.child(currentUser.getUid()).getChildren()) {
Document document = postSnapshot.getValue(Document.class);
mDocuments.add(document);
}
adapter = new ContactAdapter(getApplicationContext(), mContacts, mAuth);
recyclerView.setAdapter(adapter);
这是适配器
@Override
public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
Contact contactCurrent = mContacts.get(position);
holder.contactName.setText("Name: " + contactCurrent.getName());
holder.contactPhone.setText("Phone number: " + contactCurrent.getPhone());
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
public TextView contactName, contactPhone;
public ImageButton deleteContact;
public ContactViewHolder(@NonNull View itemView) {
super(itemView);
contactName = itemView.findViewById(R.id.contact_name);
contactPhone = itemView.findViewById(R.id.contact_phone);
deleteContact = itemView.findViewById(R.id.delete_contact);
}
}
哦,在将数据添加到 mDocuments
之前,只需使用 mDocuments.clear();
。请参阅下面的代码。
for (DataSnapshot postSnapshot : snapshot.child(currentUser.getUid()).getChildren()) {
mDocuments.clear();
Document document = postSnapshot.getValue(Document.class);
mDocuments.add(document);
}
adapter = new ContactAdapter(getApplicationContext(), mContacts, mAuth);
recyclerView.setAdapter(adapter);
所以我正在测试 Recycler View 并在添加新项目时遇到一些麻烦。
我有一个 RecyclerView,我试图在其中显示用户的联系人,但是当我创建一个新元素时,所有联系人都重复了。我知道我应该在添加新数据之前清理我的视图,但我不确定该怎么做。
这是我的主要 activity 代码
for (DataSnapshot postSnapshot : snapshot.child(currentUser.getUid()).getChildren()) {
Document document = postSnapshot.getValue(Document.class);
mDocuments.add(document);
}
adapter = new ContactAdapter(getApplicationContext(), mContacts, mAuth);
recyclerView.setAdapter(adapter);
这是适配器
@Override
public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
Contact contactCurrent = mContacts.get(position);
holder.contactName.setText("Name: " + contactCurrent.getName());
holder.contactPhone.setText("Phone number: " + contactCurrent.getPhone());
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
public TextView contactName, contactPhone;
public ImageButton deleteContact;
public ContactViewHolder(@NonNull View itemView) {
super(itemView);
contactName = itemView.findViewById(R.id.contact_name);
contactPhone = itemView.findViewById(R.id.contact_phone);
deleteContact = itemView.findViewById(R.id.delete_contact);
}
}
哦,在将数据添加到 mDocuments
之前,只需使用 mDocuments.clear();
。请参阅下面的代码。
for (DataSnapshot postSnapshot : snapshot.child(currentUser.getUid()).getChildren()) {
mDocuments.clear();
Document document = postSnapshot.getValue(Document.class);
mDocuments.add(document);
}
adapter = new ContactAdapter(getApplicationContext(), mContacts, mAuth);
recyclerView.setAdapter(adapter);