How to get text from AutoCompleteTextView?

67,077

Solution 1

arg0 being your AdapterView and arg2 the position.

Have you tried:

arg0.getItemAtPosition(arg2);

Solution 2

Yeah... unfortunately the name of the parameters on the onItemClick method you must implement are not so self-descriptive but here is an example with the names of what they are:

autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
        String selection = (String)parent.getItemAtPosition(position);
        //TODO Do something with the selected text
    }
});
  • parent The AdapterView where the click happened.
  • view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
  • position The position of the view in the adapter
  • id The row id of the item that was clicked.

For more info see: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

Solution 3

I think what you are looking for is this.

String s = this.mCountry.getEditableText().toString();

Where mCountry is the AutoCompleteTextView.

this.mCountry = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    ArrayAdapter<String> adapterCountry = new ArrayAdapter<String>(this, R.layout.list_item, countries);
    this.mCountry.setAdapter(adapterCountry);

mCountry is the list of countries, and I wanted to save the country selected in SharedPreferences.

Hope this helps.

Solution 4

Easiest of all

For Getting text of the selected suggestion in AutoCompleteTextView use this

      autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.e("========>>", autoCompleteTextView.getText().toString());
        }
    });

Solution 5

try this:

txtPurpose.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            Purpose selected = (Purpose) arg0.getAdapter().getItem(arg2);
            txtPurpose.setTag(selected);
        }
    });
Share:
67,077
madcoderz
Author by

madcoderz

My name is Ernesto Delgado. I´m currently working as a freelance Java Developer. As of now, I live in Chile but my studies were based in Sweden. I have almost 10 years of experience in Java, Hibernate, SQL, Spring, Apache Tomcat, HTML, CSS, Maven, etc. My responsibilities include the design and development of well-structurated and scalable code with Java technologies, providing technical support and knowledge transfer to my co-workers. I have worked for big companies like ENEA, Svenska Lotteriet and TV4.

Updated on May 14, 2020

Comments

  • madcoderz
    madcoderz about 4 years

    I have an AutoCompleteTextView in my app which works. I have successfully created an onClickItemListener. The question is how to grab the text the user selected.

    And this is the thing: I have an ArrayList with words being passed to the Adapter to search for suggestions. As the user types a word the suggestions list gets shorter (in rows on the UI side) so when i want to get the word from the ArrayList at the index the user selected i get the wrong word because the indexes doesn't match.

    How can I get the text (String) the user chose without having to mess with the index?

    Here's my code:

    public class AutocompleteActivity extends BaseActivity {
    
        private DBManager m_db;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.autocomplete);
    
            m_db = new DBManager(this);
            final ArrayList<String> words = m_db.selectAllWords();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, words);
    
            AutoCompleteTextView tv = (AutoCompleteTextView)findViewById(R.id.autocomplete);
            tv.setThreshold(1);
            tv.setAdapter(adapter);
    
            tv.setOnItemClickListener(new OnItemClickListener() {
    
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    Log.i("SELECTED TEXT WAS------->", words.get(arg2));
                }
            });
        }
    }