Hi folks,today I would like to share how can we create a AutoCompleteTextView with custom List.
AutoCompleteTextView widget with a simple String objects is just a simple task, but its slightly complicated with custom List. I also faced a similar problem when I tried to implement it. I would like to share how we should get the thing done in few minutes.

Lets get started.
Create a basic project in Android with the name you like. I created it with AutoCompleteDemo.

This should be your activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity”>

<AutoCompleteTextView
android:id=”@+id/txt_search”
android:hint=”@string/placeholder_search”
android:completionThreshold=”1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content” />
</RelativeLayout>

Layout for custom row in row_people.xml 

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>

<TextView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceMedium”
android:text=”Medium Text”
android:paddingLeft=”8dp”
android:paddingRight=”8dp”
android:paddingTop=”16dp”
android:paddingBottom=”16dp”
android:id=”@+id/lbl_name” />
</RelativeLayout>

Now java for the project is as follows:

package com.buzzmycode;

public class People {

private String name, lastName;
private int id;

public People(String name, String lastName, int id) {
this.name = name;
this.lastName = lastName;
this.id = id;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getlastName() {
return lastName;
}

public void setlastName(String lastName) {
this.lastName = lastName;
}
}

This is our MainActivity.java

package com.buzzmycode;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AutoCompleteTextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {

AutoCompleteTextView txtSearch;
List<People> mList;
PeopleAdapter adapter;

@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));
list.add(new People(“Sherlock”, “Holmes”, 4));
list.add(new People(“David”, “Beckham”, 5));
list.add(new People(“Bryan”, “Adams”, 6));
list.add(new People(“Arjen”, “Robben”, 7));
list.add(new People(“Van”, “Persie”, 8));
list.add(new People(“Zinedine”, “Zidane”, 9));
list.add(new People(“Luis”, “Figo”, 10));
list.add(new People(“John”, “Watson”, 11));
return list;
}
}

 

This is the most awaited Adapter class PeopleAdapter.java

package com.buzzmycode;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class PeopleAdapter extends ArrayAdapter<People> {

Context context;
int resource, textViewResourceId;
List<People> items, tempItems, suggestions;

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).getName();
return str;
}

@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
suggestions.clear();
for (People people : tempItems) {
if (people.getName().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();
}
}
}
};
}

If you like this article please comment or if you have any problems or if I have committed any mistake please let me know.