【发布时间】:2021-12-16 19:39:56
【问题描述】:
我有一个适配器,其中有两个列表,一个列表用于 InvestorsList,其中包含投资者列表,另一个列表称为 InvestorListFull,用于在搜索时过滤结果。
以下是我声明列表的方式
List<Investor> investorList;
List<Investor> investorListFull;
以下是在我的 recyclerview 适配器构造函数中分配列表的方式
public InvestorsAdapter(Context context, List<Investor> investorList) {
this.context = context;
this.investorList = investorList;
investorListFull = new ArrayList<>(investorList);
}
以下是我如何过滤投资者列表中的结果
public Filter getInvestorFilter() {
return investorFilter;
}
private final Filter investorFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Investor> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(investorListFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (Investor investor : investorListFull) {
if (investor.getUsername().toLowerCase().contains(filterPattern)) {
filteredList.add(investor);
}
if (investor.getDateJoined().toLowerCase().contains(filterPattern)) {
filteredList.add(investor);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
investorList.clear();
investorList.addAll((List) filterResults.values);
notifyDataSetChanged();
}
};
我在发布结果时遇到 Unchecked assignment 错误 InvestorList.addAll((List) filterResults.values);
【问题讨论】:
标签: java android android-studio arraylist