AutoCompleteTextView get the index of clicked item in ArrayAdapter

396 Views Asked by At

I have an AutoCompleteTextView populated from an ArrayAdapter. When the user inputs text into the AutoCompleteTextView, a drop down list of suggestions appears. After the user clicks an item, I want to get its index in the ArrayAdapter. The index is to be posted back to a webservice.

The following is the code I'm using:

List<String> listNames = new ArrayList<String>();
//listNames is populated with data from a webservice
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, listNames);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
autoComplete.setAdapter(arrayAdapter);
autoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //both print the index of the item clicked in the drop down list of suggestions
            //I want to get the index of clicked item in the arrayAdapter
            System.out.println("position " + i);
            System.out.println("id" + l);
        }
 });
0

There are 0 best solutions below