Search code examples
javaandroidsqliteandroid-cursoradapter

How can I know the position of the selected item of an AutoCompleteTextView in Android?


I have an AutoCompleteTextView. I set its adapter by extending a CursorAdapter. After selecting an option from the dropdown, I like to know the position of the item, or the item, that was selected. At least, I want an id for fetching more data in Sqlite. How can I do that?


Solution

  • Simply override the AutoCompleteTextView's OnItemClickedListener to find the position of the item in the dropdown list or the SQLite row id:

    autoCompleteTextView.setOnItemClickedListener(new OnItemSelectedListener() {
        @Override
        public void onItemClicked(AdapterView<?> parent, View view, int position, long id) {
            // Do whatever you want with the position or id
        }
    });