32

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:

ACTIVITY CODE

public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);

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

        searchBox.setAdapter(adapter);
        searchBox.setThreshold(THRESHOLD_VALUE);
        searchBox.setTextColor(Color.BLACK);
}

searchBox.setOnItemClickListener(new OnItemClickListener() {

        @Override
        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 ID,NAME,ADDRESS,MARKS.

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.

Thanks

Anshul
  • 7,914
  • 12
  • 42
  • 65

3 Answers3

93

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) {
                this.name = name;
            }
    
            @Override
            public String toString() {
                return name;
            }
    
        }
    
    AutoCompleteTextView tv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    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.setAdapter(adapter);
    
    tv.setOnItemClickListener(new OnItemClickListener() {
    
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Student selected = (Student) arg0.getAdapter().getItem(arg2);
            Toast.makeText(MainActivity.this,
                    "Clicked " + arg2 + " name: " + selected.name,
                    Toast.LENGTH_SHORT).show();
        }
    });
    
  2. Implement a custom adapter (by extending BaseAdapter class or ArrayAdapter<Student> class) Check this tutorial : http://www.ezzylearning.com/tutorial.aspx?tid=1763429

pRaNaY
  • 24,642
  • 24
  • 96
  • 146
Sameer
  • 4,379
  • 1
  • 23
  • 23
  • if I use toString() then when user selects an item from drop-down I wont get Object but the String and then I would i have to search the object using that String, right? – Anshul Oct 25 '12 at 08:35
  • 1
    You get the selected index. You can use that to get the object. – Sameer Oct 25 '12 at 08:36
  • Selected index of dropdown and AutoCompleteView will be using the String representation of Objects not actual objects? – Anshul Oct 25 '12 at 08:40
  • simple and understandable :) the best example I saw. – narb Apr 08 '17 at 11:58
  • I think this post answers the question in the best way, however: a) What is tv.setOnItemClickListener for? b) If you use Students for the adapter as presented, then the auto-suggestion box appears with the list of strings. How to render the other things in the auto-suggestion box (e.g. the student's image)? – mercury0114 Jul 22 '17 at 01:43
  • It was the toString() !! Thank the cheese's – Cheese Bread Sep 06 '19 at 21:53
8

You can use an AbstractList to get the String representation of each item in your object list.

private void setupAutoComplete(AutoCompleteTextView view, List<T> objects) {
    List<String> names = new AbstractList<String>() {
        @Override
        public int size() { return objects.size(); }

        @Override
        public String get(int location) {
            return objects.get(location).getName();
        }
    };

    view.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names));
}
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
0

You can override getItem in this adapter, depend on each type to convert it into String

 override fun getItem(position: Int): String? {
        val item = listData[position]
        when (item) {
            is BankItem -> {
                return item.bankName
            }
        ....
 }
beokh
  • 191
  • 2
  • 3