Autocomplete edit text filtering based on two different data columns

416 Views Asked by At

I am following an example for doing an autocomplete in my app,the code I am following is

@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;
}

And adapter is

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();
            }
        }
    }
};

Here the search is happening based on first name but I want search to happen based on both first name and last name.Both First name and last name should appear in the filter Kindly help,thanks in advance.

1

There are 1 best solutions below

0
On

Add one more condition while checking for name, as below

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