我目前正在学习如何使用Android Studio制作应用程序,我想制作一个AutoCompleteTextView来建议联系人,并为Contact类提供自定义适配器。
但是我的过滤器有问题: 返回之前,方法“ performFiltering”已停止,没有任何错误。 因此,方法“ publishResults”中的参数“ results”的成员属性“ values”为空。
这些是我的方法:
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<Contact> suggestions = new ArrayList<>();
if(constraint == null || constraint.length() == 0){
Log.i("ContactFiltering", "performFiltering: contrain null or 0");
suggestions.addAll(contactListFull);
}
else{
String filterPattern = constraint.toString().toLowerCase().trim();
Log.i("ContactFiltering", "performFiltering: filterPattern '" + filterPattern + "'");
for(Contact contact: contactListFull){
if(contact.name.toLowerCase().contains(filterPattern)){
suggestions.add(contact);
}
}
}
Log.i("ContactFiltering", "performFiltering: suggestions size '" + suggestions.size() + "'");
results.values = suggestions;
results.count = suggestions.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
addAll(new ArrayList<Contact>());
addAll((List) results.values);
notifyDataSetChanged();
}
“(performFiltering:建议大小'''+ Recommendations.size()+“'))”日志永远不会出现在LogCat中。
此外,我在计数器处看到for循环未到达联系人列表的末尾:
ContactFiltering:performFiltering:filterPattern'q'
ContactFiltering:performFiltering:contactListFull size'75'
ContactFiltering:performFiltering:初始计数器为0
ContactFiltering:performFiltering:计数器= 1
ContactFiltering:performFiltering:计数器= 2
ContactFiltering:performFiltering:计数器= 3
ContactFiltering:performFiltering:....
ContactFiltering:performFiltering:计数器= 28
//没什么。
我希望有人能帮助我 再见 内森。