【问题标题】:Autocomplete edit text filtering based on two different data columns基于两个不同数据列的自动完成编辑文本过滤
【发布时间】:2017-11-28 05:41:04
【问题描述】:

我正在关注一个在我的应用中进行自动完成的示例,我正在关注的代码是

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mList = retrievePeople();
    txtSearch = (AutoCompleteTextView) findViewById(R.id.txt_search);
    txtSearch.setThreshold(1);
    adapter = new PeopleAdapter(this, R.layout.activity_main, R.id.lbl_name, mList);
    txtSearch.setAdapter(adapter);
}

private List<People> retrievePeople() {
    List<People> list = new ArrayList<People>();
    list.add(new People("James", "Bond", 1));
    list.add(new People("Jason", "Bourne", 2));
    list.add(new People("Ethan", "Hunt", 3));

    return list;
}

适配器是

public PeopleAdapter(Context context, int resource, int textViewResourceId, List<People> items) {
    super(context, resource, textViewResourceId, items);
    this.context = context;
    this.resource = resource;
    this.textViewResourceId = textViewResourceId;
    this.items = items;
    tempItems = new ArrayList<People>(items); // this makes the difference.
    suggestions = new ArrayList<People>();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.row_people, parent, false);
    }
    People people = items.get(position);
    if (people != null) {
        TextView lblName = (TextView) view.findViewById(R.id.lbl_name);
        if (lblName != null)
            lblName.setText(people.getName());
    }
    return view;
}

@Override
public Filter getFilter() {
    return nameFilter;
}

/**
 * Custom Filter implementation for custom suggestions we provide.
 */
Filter nameFilter = new Filter() {
    @Override
    public CharSequence convertResultToString(Object resultValue) {
        String str = ((People) resultValue).getFirstName();
        return str;
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null) {
            suggestions.clear();
            for (People people : tempItems) {
                if (people.getFirstName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                    suggestions.add(people);
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        List<People> filterList = (ArrayList<People>) results.values;
        if (results != null && results.count > 0) {
            clear();
            for (People people : filterList) {
                add(people);
                notifyDataSetChanged();
            }
        }
    }
};

这里是根据名字进行搜索,但我希望根据名字和姓氏进行搜索。名字和姓氏都应该出现在过滤器中 请帮忙,在此先感谢。

【问题讨论】:

标签: android autocompletetextview android-filterable android-filter


【解决方案1】:

在检查名称时再添加一个条件,如下所示

for (People people : tempItems) {
     if (people.getFirstName().toLowerCase().contains(constraint.toString().toLowerCase() 
         || people.getLastName().toLowerCase().contains(constraint.toString().toLowerCase())) {
            suggestions.add(people);
      }
}

【讨论】:

    猜你喜欢
    • 2011-09-22
    • 2014-03-27
    • 2023-04-05
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2016-03-23
    相关资源
    最近更新 更多