18

I am using an Autocomplete Textview which shows some names from database.I want to show a name in a textview which I selected from autocomplete textview.Here is my code:

ArrayList<String> s1 = new ArrayList<String>();

      for (StudentInfo cn : studentInfo) {
            s1.add(cn.getName());
        }
        ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,s1); 
       a1.setThreshold(1); 
        a1.setAdapter(adapter);

a1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
    }
        });
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
bhoot4242
  • 271
  • 1
  • 2
  • 9
  • what is a1 in your code and you want to shot selected item in textview or in edittext??? – Dhaval Parmar Apr 05 '13 at 12:00
  • a1 is the autocomplete textview and I want to show selected item in textview. – bhoot4242 Apr 05 '13 at 12:12
  • in autocomplete textview when you selct item it automatically set in autocomplete textview. no need impliment extra method for that. check here: http://dj-android.blogspot.in/2013/04/android-autocompletetextview-inside.html – Dhaval Parmar Apr 05 '13 at 12:36

7 Answers7

31

Try it this way:

AutoCompleteTextView a1 = (AutoCompleteTextView) findViewById(...);

StudentInfo[] s1 = studentInfo.toArray(new StudentInfo[studentInfo.size()]);

ArrayAdapter<StudentInfo> adapter = new ArrayAdapter<StudentInfo>(this, android.R.layout.simple_dropdown_item_1line, s1);
a1.setAdapter(adapter);
a1.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
        Object item = parent.getItemAtPosition(position);
        if (item instanceof StudentInfo){
        StudentInfo student=(StudentInfo) item;
            doSomethingWith(student);
        }
    }
});

The ArrayAdapter uses the toString() method of StudentInfo to generate the displayed texts, so you need to implement a nice toString method.

This way, this kind of implementation can be adapted to any object type.

Btw.: i prefer android.R.layout.simple_spinner_dropdown_item instead of android.R.layout.simple_dropdown_item_1line

Stephan Richter
  • 1,139
  • 11
  • 31
  • Its not showing the drop down, could u help me out. – Shivang May 17 '16 at 08:59
  • 2
    parent.getItemAtPosition(position); its giving position of list shown, so position did not match with original data, its not giving desired output – Shivang May 20 '16 at 09:26
  • @Shivang is right. The real solution here: https://spapas.github.io/2019/04/05/android-custom-filter-adapter/. You need to expose the FilterResult values. – Susanta Feb 12 '20 at 09:26
  • No, `parent.getItemAtPosition(position)` works well for filtered lists. I checked that. – CoolMind Jun 02 '20 at 09:42
  • How android.R.layout.simple_spinner_dropdown_item does differ from android.R.layout.simple_dropdown_item_1line? – CoolMind Jun 02 '20 at 11:18
  • I have two autocomplete input box, If I choose the id, then automatically the value for that id needs to be set in the next autocomplete input box. {response: [{id: '', value: ''}, i{d: '', value: ''}}]} – sejn May 03 '21 at 03:34
11

Gratitude to Stefan Richter! I would like to add that it is possible to use List<T> directly when you construct the adapter:

AutoCompleteTextView autoCompleteTextView = dialogView.findViewById(R.id.autoComplete);
            // Where mStudentsInfo is List<StudentInfo>
            ArrayAdapter<StudentInfo> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, mStudentsInfo);
            autoCompleteTextView.setAdapter(adapter);
            autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Object item = parent.getItemAtPosition(position);
                    if (item instanceof StudentInfo) {
                        StudentInfo studentInfo = (StudentInfo) item;
                        // do something with the studentInfo object
                    }
                }
            });

And also do not forget to override the toString() method in StudentInfo.class:

public class StudentInfo {

....

    @Override
    public String toString() {
        return studentName;
    }
}
Ivo Stoyanov
  • 16,256
  • 8
  • 62
  • 65
4

overriding toString method for model class (StudenInfo in this case) is not a good idea!
If you only want to get the text of selected item, use this code:

 autoCompleteView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = (String) parent.getItemAtPosition(position);
                // here is your selected item
            }
        });
Ali Sherafat
  • 3,506
  • 2
  • 38
  • 51
3

your s1 contains all the names from database

a1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
            Log.d("your selected item",""+s1.get(position));
            //s1.get(position) is name selected from autocompletetextview
            // now you can show the value on textview. 
    }
        });

hope this will helps you,

Amol Sawant
  • 13,842
  • 2
  • 20
  • 27
  • 8
    NB! In this example position argument is not the position inside s1, it is index to whatever the autocomplete happened to show at the time of selection. – Anti Veeranna Apr 30 '15 at 11:41
3

From the view object arg1 get the value of the String. From the ArrayList supplied to the AutoCompleteTextView get the position of the item using this String.

in your case the code would look something like below.

int selectedPos = s1.indexOf((String) ((TextView) arg1).getText());

selectedPos is the position of the string in the supplied ArrayList.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Srikanth Reddy
  • 119
  • 1
  • 5
2

To get selected item from autocomplete selection which uses user defined datatype and sets values of related list.following code worked for me

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
    int selectedPos = getYourList().indexOf((((TextView)view).getText()).toString());
    SomeDAO dao = getSomeDaoList().get(selectedPos);
    //do your code
}

Note: I have changed default parameter names in onItemClick as arg0-parent, arg1-view,arg2-position & SomeDAO is user-defined datatype

Pahnev
  • 716
  • 5
  • 26
priyanka_rao
  • 465
  • 1
  • 4
  • 20
1

Use parent position is '0'

  txt_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int position, long id) {
            Object item = parent.getItemAtPosition(0);
            if (item instanceof MYData{
                MYData data=(MYData) item;
                String dd = data.getName();
            }
        }
    });
Anish Manchappillil
  • 697
  • 2
  • 10
  • 19