我正在尝试在自定义列表适配器中使用搜索过滤器。在我尝试查看this answer之后,我仍然面临着小问题。 我在我的代码中评论的行中存在问题。
我的适配器类
public class ContactListAdapter extends BaseAdapter implements Filterable {
private ArrayList<ContactLists> listData;
private LayoutInflater layoutInflater;
private List<String>filteredData = null;
private ItemFilter mFilter = new ItemFilter();
public ContactListAdapter(Context context, ArrayList<ContactLists> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
....
....
return convertView;
}
static class ViewHolder {
TextView name;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = listData; // I am having a problem here.
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
答案 0 :(得分:0)
因为List是字符串类型,并且您将其分配给模型的
答案 1 :(得分:0)
列表属于String
类型,listData
属于ContactLists
。确保两个阵列列表的类型相同。