42

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));
            }
        });
    }
}
Lii
  • 11,553
  • 8
  • 64
  • 88
madcoderz
  • 4,423
  • 10
  • 47
  • 74
  • Thanks for adding this code snippet, it helped for that which took me almost 6 hrs to do. +1 for that – Android Mar 09 '12 at 11:07
  • Code worked. A small tip for beginners like me, After pasting this code, click on the field and type a letter to see the suggestion box coming down. I did pasted the code, clicked on the filed and waited for the suggestion box to POPUP which will never open up. But there is showDialogBox() method to display the POPUP. – Pravinraj Venkatachalam Mar 03 '17 at 05:47

11 Answers11

63

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

Oscar Salguero
  • 10,275
  • 5
  • 49
  • 48
46

arg0 being your AdapterView and arg2 the position.

Have you tried:

arg0.getItemAtPosition(arg2);
djg
  • 1,283
  • 9
  • 6
9

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.

antyrat
  • 27,479
  • 9
  • 75
  • 76
Ammar Bukhari
  • 2,082
  • 2
  • 16
  • 16
5

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());
        }
    });
RatneZ
  • 1,078
  • 9
  • 9
3

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);
        }
    });
2

One another of getting text of suggestion selected in AutoCompleteTextView is

@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
{
    TextView txtvw=(TextView) view;
    String str=txtvw.getText().toString();
    int index = contactNames.indexOf(str);
} 
BBdev
  • 4,898
  • 2
  • 31
  • 45
Jasmine John
  • 873
  • 8
  • 12
2

Here is the code that will solve the problem.

 private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        int index = (int) view.getTag();
        Object item = parent.getItemAtPosition(index);
        if (item instanceof SearchItemShareConnectionDAO) {
            SearchItemShareConnectionDAO dao = (SearchItemShareConnectionDAO) item;

        }
    }
};

SetTag(Dao.getPosition) in getView() method of adapter.

Shivang
  • 935
  • 8
  • 18
2

To get the text of the displayed item selected by the user

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    String selectedItemText = arg0.getItemAtPosition(arg2);
    Log.i("myTag", "SELECTED TEXT WAS["+selectedItemText+"]);
}

Following best practices, please use some form of descriptive nomenclature for your variables, your code will make more sense:

public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int selectedItemIndexIn, long id) 
tony gil
  • 9,424
  • 6
  • 76
  • 100
2

There is also a way to get item outside the onItemClick:

int index = tv.getListSelection();
if (index != ListView.INVALID_POSITION) {
    Object item = tv.getAdapter().getItem(index);
}

beware that int getListSelection() may return ListView.INVALID_POSITION if there is no dropdown or if there is no selection.

YTerle
  • 2,606
  • 6
  • 24
  • 40
0

For me, but on setOnItemSelectedListener():

arg0.getSelectedItem();

was enough. The same should work on setOnItemClickListener() I guess.

Sely Lychee
  • 312
  • 1
  • 9
0

Kotlin version

autoCompleteId.setOnItemClickListener { parent, view, position, id ->
            val selectedItem = parent.getItemAtPosition(position).toString()
            Log.d("SELECTED ITEM", selectedItem )
        }

Output:

D/CLASSROOM: selectedItem 
Kwesi Welbred
  • 69
  • 1
  • 5