1

I am trying to use an AutoCompleteTextView to choose an item from a potentially long list of candidate items. I can display the list fine, and I can get the call onItemClick when the user chooses one of the autocomplete suggestions. The problem is, the "position" reported in onItemClick is the position in the dropdown list of filtered items, NOT the position in the original list of candidates.

I need to know the index of the selected item in the original list I gave to the ArrayAdapter, NOT the position in the dropdown list after it's been filtered.

I tried subclassing AutoCompleteTextView and overriding onCommitCompletion, which is supposed to give the original list index, but it is not being called when an item is selected.

I also tried subclassing BaseAdapter so I could generate the views for the autocomplete list myself and setTag with the application object for each item, but AutoCompleteTextView won't accept a BaseAdapter subclass for setAdapter.

I can't believe there isn't a way to do this without completely re-writing AutoCompleteTextView.

Hoping someone has an answer for this!

joecascio
  • 179
  • 1
  • 9
  • I know what you need here. Unfortunately your question did not get attention since you did not have code. I have posted a question, follow it. http://stackoverflow.com/questions/17425139/2-autocompletetextviews-how-to-know-which-view-has-been-selected – Siddharth Jul 02 '13 at 12:06

2 Answers2

0

I'm not entirely sure how you'd go about it, but ensure your adapter is assigning an id properly, and then use that value in the callback.

holmes
  • 578
  • 1
  • 7
  • 16
0
STATE.setOnItemClickListener(new OnItemClickListener(){ 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
        String selection = (String) parent.getItemAtPosition(position);
        int pos = -1;

        for (int i = 0; i < yourarray.length; i++) {
            if (yourarray[i].equals(selection)) {
                pos = i;
                break; 
            } 
        } 
        System.out.println("Position " + pos); //check it now in Logcat
    } 
}); 
Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60
Kabir Nazir
  • 43
  • 1
  • 4