gpt4 book ai didi

java - 我可以让 Filter getFilter() 方法更改多个列表吗?

转载 作者:行者123 更新时间:2023-12-02 02:39:05 26 4
gpt4 key购买 nike

我目前有一个带有 SearchView 的 RecyclerView,但不同之处在于,当我过滤主列表 (ArrayList) 时,我还需要更改其他两个单独的列表 (ArrayList>)。其他两个字符串不包含主列表。

现在,我可以更改主列表,但我不知道如何返回其他两个列表。

@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {

String charString = charSequence.toString();

if (charString.isEmpty()) {

filteredMainList = mainList;
filteredList1 = list1;
filteredList2 = list2;

} else {

ArrayList<String> tempFilteredMain = new ArrayList<>();
ArrayList<ArrayList<String>> tempFiltered1 = new ArrayList<>();
ArrayList<ArrayList<String>> tempFiltered2 = new ArrayList<>();

for (int i = 0; i < mainList.size(); i++) {

if (mainList.get(i).toLowerCase().contains(charString)) {

tempFilteredMain.add(mainList.get(i));
tempFiltered1.add(list1.get(i));
tempFiltered2.add(list2.get(i));
}
}

filteredMainList = tempFilteredMain;
filteredList1 = tempFiltered1;
filteredList2 = tempFiltered2;
}

//mainly starting here:

FilterResults filterResults = new FilterResults();
filterResults.values = filteredMainList;
return filterResults;
}

@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredMainList = (ArrayList<String>) filterResults.values;
notifyDataSetChanged();
}
};
}

最佳答案

如果您仍然遇到问题,可以使用以下解决方案 see here

@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();

ArrayList<ItemModel> FilteredArrList = new ArrayList<ItemModel>();
// Copy the original data

if(filter_items == null) {
filter_items = new ArrayList<ItemModel>(arrayList);
}

if(constraint == null|| constraint.length() == 0) {
results.count = filter_items.size();
results.values = filter_items;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < filter_items.size(); i++) {
if(filter_items.get(i).getName().toLowerCase().contains(constraint.toString())) {
ItemModel itemModel = new ItemModel();
itemModel.setName(filter_items.get(i).getName());
FilteredArrList.add(itemModel);
}
}
results.count = FilteredArrList.size();
results.values = FilteredArrList;

}
return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
arrayList = (ArrayList) results.values;
notifyDataSetChanged();

if(arrayList.size() == 0) {
Toast.makeText(context, "Not Found", Toast.LENGTH_LONG).show();
}

}
};
return filter;
}

关于java - 我可以让 Filter getFilter() 方法更改多个列表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57190522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com