Selected Item of Autocomplete Textview Show as simple Textview?
Asked Answered
S

8

19

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) {
    }
        });
Sedentary answered 5/4, 2013 at 11:55 Comment(3)
what is a1 in your code and you want to shot selected item in textview or in edittext???Interlocutor
a1 is the autocomplete textview and I want to show selected item in textview.Sedentary
in autocomplete textview when you selct item it automatically set in autocomplete textview. no need impliment extra method for that. check here: dj-android.blogspot.in/2013/04/…Interlocutor
L
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

Lucania answered 1/10, 2015 at 13:30 Comment(6)
Its not showing the drop down, could u help me out.Exhaustless
parent.getItemAtPosition(position); its giving position of list shown, so position did not match with original data, its not giving desired outputExhaustless
@Exhaustless is right. The real solution here: spapas.github.io/2019/04/05/android-custom-filter-adapter. You need to expose the FilterResult values.Adhesive
No, parent.getItemAtPosition(position) works well for filtered lists. I checked that.Neff
How android.R.layout.simple_spinner_dropdown_item does differ from android.R.layout.simple_dropdown_item_1line?Neff
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: ''}}]}Nasion
F
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;
    }
}
Fdic answered 21/12, 2017 at 11:27 Comment(3)
Can item instanceof StudentInfo be false? ArrayAdapter<StudentInfo> sets all items as StudentInfo.Neff
@CoolMind, I always check before cast. Always! :)Fdic
A believe you, but what is a condition to get item not an instance of StudentInfo?Neff
N
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
            }
        });
Netti answered 14/7, 2016 at 20:6 Comment(0)
C
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,

Claim answered 5/4, 2013 at 12:16 Comment(1)
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.Garbe
A
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.

Antaeus answered 29/4, 2014 at 9:0 Comment(0)
D
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

Dissected answered 23/6, 2014 at 11:6 Comment(0)
T
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();
            }
        }
    });
Tortile answered 8/7, 2017 at 8:52 Comment(0)
H
-1
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();
            }
        }
    });
Hyksos answered 29/5 at 15:13 Comment(2)
The code in this code-only answer is copied exactly from this answer to this question by Anish Manchappillil without attribution or modification, other than changing the indentation of the first line.Kalimantan
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Bacchae

© 2022 - 2024 — McMap. All rights reserved.