我创建了一个自定义AutoCompleteTextView适配器。第一次调用performFiltering()时,它会过滤结果并返回FilterResults的实例(具有正确的值)。之后系统调用PublishResults()方法,该方法调用notifyDataSetChanged()。
在调用notifyDataSetChanged()之后,立即再次调用performFiltering方法。返回结果后,它不再调用PublishResults()。
我还应该注意,过滤后的结果不会传递给AutoComplete小部件。相反,整个原始数据集显示在下拉列表中。并且值得注意的是,即使我将窗口小部件上的阈值设置为2,也会在输入第一个字符后调用performFiltering()。
所以我的问题是:(1)为什么在notifyDataSetChanged()之后第二次调用performFiltering()?,(2)为什么在满足阈值之前调用performFiltering()?,以及(3)最常见的是什么筛选结果的原因没有进入下拉列表?
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;
import android.widget.Filterable;
import android.widget.Filter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by John on 12/31/2016.
*/
public class myContactsAutoCompleteAdapter extends BaseAdapter
implements Filterable {
Context context;
private LayoutInflater inflator;
private ArrayList<Contacts.ContactDataRow> contactsList;
private ArrayList<Contacts.ContactDataRow> suggestions = new ArrayList<>();
public myContactsAutoCompleteAdapter(Context ctx, ArrayList<Contacts.ContactDataRow> data ) {
this.context = ctx;
this.contactsList = data;
this.inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return contactsList.size();
}
@Override
public Object getItem(int position) {
return contactsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflator.inflate(R.layout.contacts_listview_item, null);
viewHolder = new ViewHolder();
viewHolder.txt_name = (TextView) convertView.findViewById(R.id.txt_contact_name);
viewHolder.txt_phone = (TextView) convertView.findViewById(R.id.txt_contact_phone);
viewHolder.txt_phonetype = (TextView) convertView.findViewById(R.id.txt_contact_type);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_name.setText(contactsList.get(position).displayName);
viewHolder.txt_phone.setText(contactsList.get(position).phone);
viewHolder.txt_phonetype.setText(contactsList.get(position).phoneType);
return convertView;
}
private static class ViewHolder {
TextView txt_name;
TextView txt_phone;
TextView txt_phonetype;
}
@Override
public Filter getFilter() {
return new ContactsAutocompleteFilter();
}
private class ContactsAutocompleteFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null && constraint.length() >= 2) {
suggestions.clear();
//Check for similarities in data from constraint
for (Contacts.ContactDataRow row : contactsList) {
if (row.displayName.toLowerCase().contains(constraint.toString().toLowerCase())) {
suggestions.add(row);
}
}
filterResults.values = suggestions;
filterResults.count = suggestions.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
编辑:我更新了这个以包含整个适配器类