任何人都可以解释一下,为什么在此部分代码中重写List connectionsCategories?我试图过滤放置在RecyclerView内的Filter的performFiltering方法内的数据。如我所见,该列表在此行中采用了新值:connectionsCategoryModelCopy.setValues(filteredConnectionsModels);
String charString = charSequence.toString();
filteredConnectionsCategories.clear();
if (charString.isEmpty()) {
filteredConnectionsCategories.addAll(connectionsCategories);
} else {
List<ConnectionsCategoryModel> filteredList = new ArrayList<>();
for (ConnectionsCategoryModel connectionsCategoryModel : connectionsCategories) {
ConnectionsCategoryModel connectionsCategoryModelCopy = connectionsCategoryModel;
List<ConnectionsModel> connectionsModels = connectionsCategoryModelCopy.getValues();
List<ConnectionsModel> filteredConnectionsModels = new ArrayList<>();
for (ConnectionsModel connectionsModel : connectionsModels) {
if (connectionsModel.getName().toLowerCase().replaceAll("\\s", "").contains(charString.toLowerCase().replaceAll("\\s", ""))) {
filteredConnectionsModels.add(connectionsModel);
}
}
if (!filteredConnectionsModels.isEmpty()) {
connectionsCategoryModelCopy.setValues(filteredConnectionsModels);
filteredList.add(connectionsCategoryModelCopy);
}
}
filteredConnectionsCategories = filteredList;
}
答案 0 :(得分:1)
ConnectionsCategoryModel connectionsCategoryModelCopy = connectionsCategoryModel;
这行是您的问题。您正在此处制作“浅表副本”。当您获得此列表时:
List<ConnectionsModel> connectionsModels = connectionsCategoryModelCopy.getValues();
在进行替换调用之前,您需要对每个元素进行“深度复制”。