我有一个从ArrayAdapter填充的AutoCompleteTextView。当用户将文本输入AutoCompleteTextView时,会出现一个下拉列表。用户单击一个项后,我想在ArrayAdapter中获取其索引。该索引将被发布回Web服务。
以下是我使用的代码:
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);
}
});