Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 447
Anshul Avatar asked Oct 25 '12 07:10

Anshul


People also ask

Which method is used for filling the data to AutoCompleteTextView?

Android AutoCompleteTextView Overview Some important methods of Autocomplete list are given below: getAdapter() : This method returns a filterable list adapter used for auto completion.

What is Android Completionhint attribute in AutoCompleteTextView?

Defines the hint view displayed in the drop down menu. Defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu.

How do I set autocomplete text on Android?

If you want to get suggestions , when you type in an editable text field , you can do this via AutoCompleteTextView. It provides suggestions automatically when the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

How do I create an auto-complete text view field in XML?

In android, we can create an AutoCompleteTextView control in two ways either manually in an XML file or create it in the Activity file programmatically. First we create a new project by following the below steps: Click on File, then New => New Project. After that include the Kotlin support and click on next.


1 Answers

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

like image 71
Sameer Avatar answered Oct 20 '22 14:10

Sameer