anks anks - 2 years ago 120
Android Question

Using Android AutoCompleteTextView with ArrayAdapter<Objects> instead of ArrayAdapter<Strings>

I wanted to use AutoCompleteTextView in my android application.I know how to use it with simple array of Strings, but I wanted AutoCompleteTextView to use list of Objects to perform completion.My code for this is following:


public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub

ArrayAdapter<Student> adapter = new ArrayAdapter<Student>(this,
R.layout.dropdown_list_item, GetAllStudentsList());


searchBox.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> adapterView, View view,
int position, long arg3) {
//Here i will grab the Student object that user selected from drop-down



public ArrayList<Movies> GetAllStudentsList() {

//This method returns a ArrayList of <Student> type objects

Student class Object has information regarding a student which is

I know AutoCompleteTextView needs an array of String type object to perform search operation.In my case I want AutoCompleteTextView to use my ArrayList to perform completion on the basis of Student object field NAME.I dont know how should I specify AutoCompleteTextView to use NAME field of Student object.Please help me providing any Link or a small example.


Answer Source

Two ways:

  1. Override toString() in Student class and make it return name. You can get the object that was selected with the following code:

     public static class Student {
        private String name;
            public Student(String name) {
       = name;
            public String toString() {
                return name;
    AutoCompleteTextView tv = (AutoCompleteTextView) findViewById(;
    final ArrayList<Student> list = new ArrayList<MainActivity.Student>();
    list.add(new Student("Viru"));
    list.add(new Student("Gauti"));
    ArrayAdapter<Student> adapter = new ArrayAdapter<MainActivity.Student>(
            this, android.R.layout.simple_dropdown_item_1line, list);
    tv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Student selected = (Student) arg0.getAdapter().getItem(arg2);
                    "Clicked " + arg2 + " name: " +,
  2. Implement a custom adapter (by extending BaseAdapter class or ArrayAdapter<Student> class) Check this tutorial :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download